Monday, 24 June 2024

Laravel Model tutorials

create a laravel project for my table spends with columns 

id bigint autoincre 

 date date 

description varchar(150) 

amount float. Use Model, migration, controller with all resources and views.


Solution:

Create the Migration: Use the make:migration command to create a migration file for the spends table:

php artisan make:migration create_spends_table
 

Define the Schema: Open the generated migration file in database/migrations and define the schema for the spends table:

 

// database/migrations/xxxx_xx_xx_xxxxxx_create_spends_table.php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSpendsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('spends', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->date('date');
            $table->string('description', 150);
            $table->float('amount');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('spends');
    }
}
 

Run the Migration: Apply the migration to create the spends table in your database:

php artisan migrate
 

Create the Model: Use the make:model command to create a model for the spends table:

php artisan make:model Spend
 

Define Fillable Attributes: Open the model file created in app/Models/Spend.php and define the fillable attributes:

// app/Models/Spend.php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Spend extends Model
{
    use HasFactory;

    protected $fillable = [
        'date',
        'description',
        'amount',
    ];
}

Define the Controller Methods: Open the generated controller file in app/Http/Controllers/SpendController.php and add the methods for CRUD operations:

// app/Http/Controllers/SpendController.php

namespace App\Http\Controllers;

use App\Models\Spend;
use Illuminate\Http\Request;

class SpendController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $spends = Spend::all();
        return response()->json($spends);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'date' => 'required|date',
            'description' => 'required|string|max:150',
            'amount' => 'required|numeric',
        ]);

        $spend = Spend::create($request->all());

        return response()->json($spend, 201);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Spend  $spend
     * @return \Illuminate\Http\Response
     */
    public function show(Spend $spend)
    {
        return response()->json($spend);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Spend  $spend
     * @return \Illuminate\Http\Response
     */
    public function edit(Spend $spend)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Spend  $spend
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Spend $spend)
    {
        $request->validate([
            'date' => 'required|date',
            'description' => 'required|string|max:150',
            'amount' => 'required|numeric',
        ]);

        $spend->update($request->all());

        return response()->json($spend);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Spend  $spend
     * @return \Illuminate\Http\Response
     */
    public function destroy(Spend $spend)
    {
        $spend->delete();

        return response()->json(null, 204);
    }
}

Define the Routes: Open your routes file (typically routes/web.php for web routes or routes/api.php for API routes) and add the routes for the SpendController:

// routes/web.php or routes/api.php

use App\Http\Controllers\SpendController;

Route::resource('spends', SpendController::class);

Create the Blade View for Listing Data: Create a Blade view to list all the spends. Let's name it index.blade.php.

<!-- resources/views/spends/index.blade.php -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spends List</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
    <div class="container mt-5">
        <h1>Spends List</h1>
        <a href="{{ route('spends.create') }}" class="btn btn-primary mb-3">Add Spend</a>
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Date</th>
                    <th>Description</th>
                    <th>Amount</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                @foreach ($spends as $spend)
                    <tr>
                        <td>{{ $spend->id }}</td>
                        <td>{{ $spend->date }}</td>
                        <td>{{ $spend->description }}</td>
                        <td>{{ $spend->amount }}</td>
                        <td>
                            <a href="{{ route('spends.edit', $spend->id) }}" class="btn btn-warning btn-sm">Edit</a>
                            <form action="{{ route('spends.destroy', $spend->id) }}" method="POST" style="display:inline;">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="btn btn-danger btn-sm">Delete</button>
                            </form>
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</body>
</html>


Create the Blade View for Inserting Data: Create a Blade view to insert new spends. Let's name it create.blade.php.

<!-- resources/views/spends/create.blade.php -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Add Spend</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
    <div class="container mt-5">
        <h1>Add Spend</h1>
        <form action="{{ route('spends.store') }}" method="POST">
            @csrf
            <div class="form-group">
                <label for="date">Date</label>
                <input type="date" name="date" id="date" class="form-control" required>
            </div>
            <div class="form-group">
                <label for="description">Description</label>
                <input type="text" name="description" id="description" class="form-control" maxlength="150" required>
            </div>
            <div class="form-group">
                <label for="amount">Amount</label>
                <input type="number" name="amount" id="amount" class="form-control" step="0.01" required>
            </div>
            <button type="submit" class="btn btn-success">Add Spend</button>
            <a href="{{ route('spends.index') }}" class="btn btn-secondary">Back to List</a>
        </form>
    </div>
</body>
</html>
 

 

Update the Controller to Return Views: Update the SpendController to return these views.

// app/Http/Controllers/SpendController.php

namespace App\Http\Controllers;

use App\Models\Spend;
use Illuminate\Http\Request;

class SpendController extends Controller
{
    // Other methods...

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $spends = Spend::all();
        return view('spends.index', compact('spends'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('spends.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'date' => 'required|date',
            'description' => 'required|string|max:150',
            'amount' => 'required|numeric',
        ]);

        Spend::create($request->all());

        return redirect()->route('spends.index')->with('success', 'Spend added successfully.');
    }

    // Other methods...
}
 

Create the Blade View for Editing Data: Create a Blade view to edit an existing spend. Let's name it edit.blade.php.

<!-- resources/views/spends/edit.blade.php -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Spend</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
    <div class="container mt-5">
        <h1>Edit Spend</h1>
        <form action="{{ route('spends.update', $spend->id) }}" method="POST">
            @csrf
            @method('PUT')
            <div class="form-group">
                <label for="date">Date</label>
                <input type="date" name="date" id="date" class="form-control" value="{{ $spend->date }}" required>
            </div>
            <div class="form-group">
                <label for="description">Description</label>
                <input type="text" name="description" id="description" class="form-control" maxlength="150" value="{{ $spend->description }}" required>
            </div>
            <div class="form-group">
                <label for="amount">Amount</label>
                <input type="number" name="amount" id="amount" class="form-control" step="0.01" value="{{ $spend->amount }}" required>
            </div>
            <button type="submit" class="btn btn-success">Update Spend</button>
            <a href="{{ route('spends.index') }}" class="btn btn-secondary">Back to List</a>
        </form>
    </div>
</body>
</html>
 

 Update the Controller to Return the Edit View and Handle the Update: Open your SpendController and add the edit and update methods.

// app/Http/Controllers/SpendController.php

namespace App\Http\Controllers;

use App\Models\Spend;
use Illuminate\Http\Request;

class SpendController extends Controller
{
    // Other methods...

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Spend  $spend
     * @return \Illuminate\Http\Response
     */
    public function edit(Spend $spend)
    {
        return view('spends.edit', compact('spend'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Spend  $spend
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Spend $spend)
    {
        $request->validate([
            'date' => 'required|date',
            'description' => 'required|string|max:150',
            'amount' => 'required|numeric',
        ]);

        $spend->update($request->only(['date', 'description', 'amount']));

        return redirect()->route('spends.index')->with('success', 'Spend updated successfully.');
    }

    // Other methods...
}
 

 

 

 

 

 

 

 

 

 

 

 


Sunday, 1 November 2020

 

 

PRACTICE SET



SUBJECT- GENERAL SCIENCE CLASS-X


SECTION-A (25 MARKS)


Q) Choose the correct answer:-


1) ZnO + C → Zn + CO is an example of

(i) Combination Reaction.

(ii) Double Displacement Reaction.

(iii) Redox Reaction.

(iv) Decomposition Reaction.

2) The breakdown of pyruvate to give Carbon di Oxide, water and energy takes place in

(i) Cytoplasm.

(ii) Mitochondria.

(iii) Chloroplast.

(iv) Nucleus.


3) No matter how far you stand from a mirror, your image appears erect. The mirror is likely to be

(i) Plane.

(ii) Concave.

(iii) Convex.

(iv) Either Plane or Convex.


4) Fe2O3 + 2Al → Al2O3 + 2Fe is an example of

(i) Combination Reaction.

(ii) Decomposition Reaction.

(iii) Displacement Reaction.

(iv) Double Displacement Reaction.


5) A solution turns red litmus blue. The pH of the solution is

(i) 1

(ii) 4

(iii) 5

(iv) 10


6) In the periodic table of the elements, in a period from left to right atomic radius gradually

(i) Increases

(ii) Decreases

(iii) Remains same

(iv) None of these


7) The part of the brain which is responsible for maintaining equilibrium & posture of the body is

(i) Cerebellum

(ii) Medulla

(iii) Pons

(iv) Cerebrum


8) The organs having different origin, similar appearance and similar function are called

(i) Homologous

(ii) Analogous

(iii) Adjacent

(iv) Vestigial


9) A person cannot see distinctly objects kept beyond 2m. This defect can be corrected by using a lens of power

(i) -0.2D

(ii) +0.2D

(iii) -0.5D

(iv) +0.5D

Explaination:


Given distance of far point (D) = 2m
Let f be the focal length of eye lens.
Image distance ( distance between eye lens and retina ) for our human eye is always constant and is about 2.5 cm.
This is due to the action of ciliary muscles.
applying lens formula, we get
f = -D
= -2 cm.
but , the power of a lens is reciprocal of focal length.
hence. Power = 1/f. ( f in meters )
= 1/-2
= - 0.5 dioptre.

10) Which of the following is not a conventional source of energy?

(i) Fossil Fuels

(ii) Bio-Mass

(iii) Nuclear Energy

(iv) Wind Energy


11) What happens when dilute HCl is added to iron filings?

(i) Iron salt & water are produced

(ii) No reaction takes place

(iii) Chlorine gas & iron hydroxide are produced

(iv) Hydrogen gas & iron chloride are produced


12) The reaction of lime with water is a

(i) Decomposition reaction

(ii) Displacement reaction

(iii) Double displacement reaction

(iv) Combination reaction


13) Which of the following metals can replace copper from a solution of copper sulphate?

(i) Silver

(ii) Gold

(iii) Zinc

(iv) Mercury


14) A solution reacts with crushed egg-shells to give a gas that turns lime water milky. The solution contains

(i) NaCl

(ii) HCl

(iii) LiCl

(iv) KCl


15) Which one of the following types of medicines is used for treating indigestion?

(i) Antibiotic

(ii) Analgestic

(iii) Antacid

(iv) Antiseptic


16) The acid found in curd is

(i) Oxalic acid

(ii) Tartaric acid

(iii) Lactic acid

(iv) Citric acid


17) The taste of soap solution is bitter in taste because soap solution is a

(i) Alkaline

(ii) Acidic

(iii) Both (i) & (ii)

(iv) None of these


18) According to Mendeleev’s Periodic Law, the elements were arranged in the periodic table in the order of

(i) Increasing atomic masses

(ii) Decreasing atomic masses

(iii) Increasing atomic number

(iv) Decreasing atomic number


19) An element which is an essential constituent of all organic compounds belongs to

(i) Group 1

(ii) Group 16

(iii) Group 14

(iv) Group 15


20) Which of the following element would lose an electron easily?

(i) Mg

(ii) Na

(iii) K

(iv) Ca


21) Which of the following element does not lose an electron easily?

(i) Na

(ii) Al

(iii) F

(iv) Mg


22) Emulsification of fat molecule is carried out by

(i) Trypsin

(ii) Pepsin

(iii) Bile salts

(iv) Amylase

23) Anaerobic respiration occurs in

(i) Mitochondria

(ii) Chloroplast

(iii) Nucleus

(iv) Cytoplasm


24) The enzyme which converts starch into simple sugar is

(i) Amylase

(ii) Lipase

(iii) Pepsin

(iv) Trypsin


25) The name of the process with the help of which carbon & energy requirements of an autotrophic organism are fulfilled, is

(i) Photosynthesis

(ii) Transpiration

(iii) Translocation

(iv) Photophosphorylation


26) Largest cell of the body

(i) Muscle Cell

(ii) Blood Cell

(iii) Nerve Cell

(iv) Epithelial Cell


27) The intermediate space between two adjacent cells is called

(i) Synapse

(ii) Axon

(iii) Dendrite

(iv) Cellular Space


28) The number of cranial nerves in mammals is

(i) 12 pairs

(ii) 24 pairs

(iii) 36 pairs

(iv) 48 pairs


29) The number of spinal nerves in mammals is

(i) 32 pairs

(ii) 31 pairs

(iii) 21 pairs

(iv) 20 pairs


30) Which of the following is the genetic materials of organisms?

(i) DNA

(ii) Protein

(iii) Carbohydrate

(iv) Chromosome


31) Exchange of genetic material takes place in

(i) Sexual Reproduction

(ii) Budding

(iii) Asexual Reproduction

(iv) Vegetative Propagation


32) The maleness of a child is determined by

(i) Sex is determined by chance

(ii) The cytoplasm of green cell which determines the sex

(iii) The X chromosome in zygote

(iv) The Y chromosome in zygote


33) A trait in an organism is influenced by

(i) Maternal DNA

(ii) Paternal DNA

(iii) Both of these

(iv) None of these


34) Velocity of light is maximum in

(i) Air

(ii) Vacuum

(iii) Water

(iv) None of these


35) The real inverted & equal to the object image is produced if the object is placed before concave mirror

(i) At C

(ii) At F

(iii) Between F & C

(iv) Between F & P


36) An image is formed by a concave lens. The image is

(i) Real & Diminished

(ii) Virtual & Diminished

(iii) Real & Magnified

(iv) Virtual & Magnified


37) If an object is placed between the reflecting surfaces of two plane mirrors joined at an angle 90°, the number of images formed is

(i) Two

(ii) Three

(iii) Four

(iv) Numerous


38) The least distance of distinct vision for a young adult with normal vision is about

(i) 25 m

(ii) 25 cm

(iii) 2.5 cm

(iv) 2.5 m


39) The change of focal length of an eye lens is caused by the action of the

(i) Pupil

(ii) Retina

(iii) Ciliary Muscles

(iv) Iris


40) When light rays enter the eye, most of the refraction occurs at the

(i) Iris

(ii) Pupil

(iii) Outer surface of the Cornea

(iv) Crystalline lens


41) Human eye is more or less like a

(i) Microscope

(ii) Telescope

(iii) Photographic Camera

(iv) None of these


42) Which of the following is not derived from energy of the Sun?

(i) Biomass

(ii) Nuclear Energy

(iii) Geothermal Energy

(iv) Wind Energy


43) Which of the following is not an example of a bio-mass energy source?

(i) Wood

(ii) Nuclear Energy

(iii) Gobar Gas

(iv) Coal


44) The product of petroleum which have greatest demand is

(i) Kerosene

(ii) Gasoline

(iii) Diesel

(iv) Fuel Oil


45) The amount of carbon present in Anthracite Coal is

(i) 65%

(ii) 78%

(iii) 95%

(iv) 99%


46) Which of the following mirror is used as rear view of cars?

(i) Plane mirror

(ii) Convex mirror

(iii) Concave mirror

(iv) None of these


47) The wavelength of yellow light of sodium bulb is

(i) 0.59 m

(ii) 59 m

(iii) 5.9 m

(iv) 0.059 m


48) If an image is formed at focus of a concave mirror, then the image will be

(i) Real & Inverted

(ii) Virtual & Erect

(iii) Real & Erect

(iv) Virtual & Inverted


49) Which of the following disaccharide is found in milk?

(i) Lactose

(ii) Maltose

(iii) Sucrose

(iv) None of these


50) Which of the following helps in the formation of Chlorophyll?

(i) Sodium

(ii) Magnesium

(iii) Calcium

(iv) None of these


SECTION-B (PROJECT 25 MARKS)


Draw any three ray diagrams using Concave mirror & any three diagrams using Convex lens. Also answer the following questions:-

(i) What is Principal Axis?

(ii) What is the “Pole” of the mirror?

(iii) What is the “Optical Centre” of the lens?

(iv) What is Aperture?

(v) What is Focal Length?

(vi) State the Laws of Reflection.

(vii) State the Laws of Refraction.

(viii) What is Refractive Index & how it is denoted?

(ix) What is Absolute Refractive Index?

(x) What is Rarer & Denser medium?

(xi) Why Concave Lens forms a Virtual image?

(xii) Write the Mirror formula & Lens formula.

(xiii) What are the Centre & Radius of Curvature?

(xiv) How many types of lenses are there?

(xv) Differentiate between Reflection & Refraction.


**************************************