Valar M
Valar M

Reputation: 51

How to get all records in laravel Models

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?

Upvotes: 2

Views: 14738

Answers (3)

Rusben Guzman
Rusben Guzman

Reputation: 402

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

Upvotes: 2

AlexR1712
AlexR1712

Reputation: 59

Another way is using DB Facade with joins: Like this:

$users = DB::table('users')
        ->join('contacts', 'users.id', '=', 'contacts.user_id')
        ->join('orders', 'users.id', '=', 'orders.user_id')
        ->select('users.*', 'contacts.phone', 'orders.price')
        ->get();

Upvotes: 0

Haris
Haris

Reputation: 111

The Good way to Solve this is laravel relations here the link laravel documentation

Select your driver table as base table and use relations to get the other table fields; array_push() function to push values to array

Upvotes: 0

Related Questions