How to get all records in laravel Models

10,536

Laravel offers two very useful tools for performing database operations eloquent and query builder. It is advisable to work as much as possible with eloquent and their relationships as it facilitates much of the common operations that we normally need to perform.

Now, if you want to make more complex queries you can use query builder, also, an example of your case using query builder would be something like this:

In your controller you make the query and pass the data to view:

$data = DB::table('driver')
          ->leftJoin('bank', 'bank.driver_id','=', 'driver.driver_id')
          ->join('tyre', 'tyre.id','=', 'bank.tyre_id')
          ->select('driver.driver_id as id',
                   'driver.name',
                   'bank.id as bank_id',
                   'tyre.title')
          ->groupBy('driver.driver_id')
          ->get()

And in your view you can use a foreach loop to display the data (and you can use a conditional to display the bank field):

 <table>
    <thead>
        <tr>
            <th>Driver ID</th>
            <th>Name</th>
            <th>Bank Detail</th>
            <th>Tyre Title</th>
        </tr>
    </thead>
    <tbody>
        @foreach($data as $item)
            <tr>
                <td>{{ $item->id }}</td>
                <td>{{ $item->name }}</td>
                <td>{{ isset($item->bank_id) ? "availible":"N/A" }}</td>
                <td>{{ $item->title }}</td>
            </tr>                     
        @endforeach
    </tbody>
 </table>

likewise I recommend you read the documentation of eloquent and try to use it.

https://laravel.com/docs/5.5/queries

https://laravel.com/docs/5.5/eloquent

Share:
10,536
Valar M
Author by

Valar M

WordPress Developer.

Updated on June 14, 2022

Comments

  • Valar M
    Valar M almost 2 years

    I'm not using eloquent, my models are like this.

    class drivers extends Model
    {
    }
    

    I want to display records of all drivers ( one record in each row )

    My driver table has field (driver_id,name,tyre_id)

    My tyre table has field (id, title)

    My bank table has field (id, driver_id, bank)

    I want my record to be like this...

    Driver Id, Name, Bank Details, Tyre Title

    100000111, Ayer, Available, GO

    .. so on

    For bank details if driver_id has a record in bank table, it should display available otherwise N/A.

    $drivers= Drivers::get();
    $myarray = ();
    foreach ($drivers as $d){
        $bank = Bank::where('driver_id',$d->driver_id)->first();
        $tyre = Tyre::where('id',$d->tyre_id)->first();
        $myarray[]  = $d->driver_id;
        $myarray[]  = $d->name;
        $myarray[]  = isset($bank) ? "available" ; '';
        $myarray[]  = $tyre->title;
    
    }
    

    This is what i have tried, I'm to new to laravel, how can i achieve this in laravel or using query like DB Table?

    • Option
      Option over 6 years
      Ok so you need to basically either use Joins or Relationships in order to join the tables together. Also, you don't want to run ->first() as this will only fetch 1 result you need to run ->get(); and then use a foreach() loop in your blade file. I can only assume id in tyre matches the driver_id ??