Select Query in Laravel using Eloquent ORM | Laravel 9

Select Query in Laravel using Eloquent ORM

Create Controller

Open new terminal and run this command to create controller


  php artisan make:controller customerController
  

Configure routes

Go to routes>web.php


use App\Http\Controllers\customerController;

Route::get('/customer', [customerController::class, 'index']);
Route::post('/customer', [customerController::class, 'store']);
Route::get('/customer/view', [customerController::class, 'view']);
  


index is a function which return customer page and store is a function which store the data submitted through form. view is a function which fetch and view the data to the page, as given below.

App>Http>Controllers:


";
        print_r($request->all());

        $customers = new Customers;
        $customers->name = $request['name'];
        $customers->email = $request['email'];
        $customers->password = md5($request['password']);
        $customers->city = $request['city'];
        $customers->state = $request['state'];
        $customers->address = $request['address'];
        $customers->gender = $request['gender'];
        $customers->dob = $request['dob'];
        $customers->save();

        return redirect('/customer/view');
    }

    public function view(){
        $customers = Customers::all();
        // echo "
";
        // print_r($customers->toArray());
        // echo "
"; $data = compact('customers'); return view('customer-view')->with($data); } }

Above code is use to insert data. $customers->name is a database name and $request['name']; is a name which is present in input tag.

In the view function Customers::all() collect the data, the compact function convert it into the array and with sends it to customer.blade.php

Note - Target table using - use App\Models\Customers; because table name is customers in the database>migration>customers_table.

App>Models>Customers.php



  namespace App\Models;

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

  class Customers extends Model
  {
      use HasFactory;
      protected $table = "customers";
      protected $primaryKey = "customer_id";
  }

The Table is given below

Create a file inside resources>views>customer-view.blade.php

<div class="container my-4">
    <div class="table-responsive">
        
                @foreach ($customers as $customer)
                
                @endforeach
            <table class="table table-dark">
            <thead>
                <tr>
                    <th scope="col">Name</th>
                    <th scope="col">Email</th>
                    <th scope="col">Gender</th>
                    <th scope="col">State</th>
                    <th scope="col">City</th>
                    <th scope="col">DOB</th>
                    <th scope="col">Status</th>
                </tr>
            </thead>
            <tbody><tr>
                    <td>{{$customer-&gt;name}}</td>
                    <td>{{$customer-&gt;email}}</td>
                    <td>
                        @if ($customer-&gt;gender == "M")
                            Male
                        @elseif($customer-&gt;gender == "F")
                            Female
                        @else
                            Other
                        @endif
                    </td>
                    <td>{{$customer-&gt;state}}</td>
                    <td>{{$customer-&gt;city}}</td>
                    <td>{{$customer-&gt;dob}}</td>
                    <td>
                        @if ($customer-&gt;status == "1")
                            Active
                        @else
                            Inactive
                        @endif
                    </td>
                </tr></tbody>
        </table><br /></div></div>
        

resources>views>customer.blade.php The Form is given below Create a file inside



<div class="container">
        <h2 class="text-center">Customer Registration</h2>

            <form action="{{url('/')}}/customer" class="row g-3" method="post">
                @csrf
                <div class="col-md-6">
                  <label class="form-label">Name</label>
                  <input class="form-control" id="inputEmail4" name="name" type="text" value="{{old('name')}}" />
                </div>
                <div class="col-md-6">
                  <label class="form-label">Email</label>
                  <input class="form-control" id="inputPassword4" name="email" type="email" value="{{old('email')}}" />
                </div>
                <div class="col-md-6">
                  <label class="form-label">Password</label>
                  <input class="form-control" id="inputEmail4" name="password" type="password" />
                </div>
                <div class="col-md-6">
                  <label class="form-label">Confirm Password</label>
                  <input class="form-control" id="inputPassword4" name="cpassword" type="password" />
                </div>
                <div class="col-md-6">
                  <label class="form-label">City</label>
                  <input class="form-control" id="inputEmail4" name="city" type="text" value="{{old('city')}}" />
                </div>
                <div class="col-md-6">
                  <label class="form-label">State</label>
                  <input class="form-control" id="inputPassword4" name="state" type="text" value="{{old('state')}}" />
                </div>
                <div class="col-12">
                  <label class="form-label">Address</label>
                  <input class="form-control" id="inputAddress2" name="address" type="text" value="{{old('')}}" />
                </div>
                <div class="col-md-6">
                  <label class="form-label">Gender: </label>
                  <div class="form-check">
                    <input class="form-check-input" id="inlineRadio1" name="gender" type="radio" value="M" />
                    <label class="form-check-label">Male</label>
                  </div>
                  <div class="form-check">
                    <input class="form-check-input" id="inlineRadio2" name="gender" type="radio" value="F" />
                    <label class="form-check-label">Female</label>
                  </div>
                  <div class="form-check">
                    <input class="form-check-input" id="inlineRadio3" name="gender" type="radio" value="O" />
                    <label class="form-check-label">Other</label>
                  </div>
                </div>
                <div class="col-md-4">
                  <label class="form-label">Date of Birth</label>
                  <input class="form-control" id="inputCity" name="dob" type="date" />
                </div>
                <div class="col-12">
                  <button class="btn btn-primary" type="submit">Submit</button>
                </div>
              </form>
          </div>

Comments