How to select columns from joined tables: laravel eloquent

Anil P Babu

I have a different problem from this. The scenario is same but I am in need of more filtration of the results.

Let me explain.

Consider I have 2 tables

vehicles

 id
 name
 staff_id
 distance
 mileage

staffs

 id
 name
 designation

I want to select only id and name from both tables(Models). The Vehicle Model contain a belongsTo relation to Staff model.

class Vehicle extends Model
{
    public function staff()
    {
      return $this->belongsTo('App\Staff','staff_id');
    }
}

and I joined using this

Vehicle::where('id',1)
            ->with(['staff'=> function($query){
                            // selecting fields from staff table
                            $query->select(['staff.id','staff.name']);
                          }])
            ->get();

When I put fields in ->get() like this

->get(['id','name'])

it filters the vehicle table but produce no result of Staff table.

Any ideas?

Anil P Babu

Finally found it.. In the ->get() you have to put the 'staff_id' like this

Vehicle::where('id',1)
            ->with(['staff'=> function($query){
                            // selecting fields from staff table
                            $query->select(['staff.id','staff.name']);
                          }])
            ->get(['id','name','staff_id']);

Since I didn't take the staff_id, it couldn't perform the join and hence staff table fields were not shown.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to conditionally select a field from joined tables in SQL?

How to select specific columns in laravel eloquent

Laravel Eloquent: getting id field of joined tables in Eloquent

laravel 5.1 eloquent select with prefix joined table

Select Sum from two joined tables

Select as JSON joined tables

Select Max From 2 Tables Joined

How to select in Laravel 5.6 Eloquent with parameters on multiple tables?

Get columns from joined tables as rows instead of adjacent columns

How to get adjacent columns of joined tables in rows instead of columns?

How to get data from Multiple Tables with Laravel Eloquent

Laravel Eloquent 'withCount' function on multiple joined tables

Select Min Datediff from joined tables

Select only specific columns from joined tables (Many-to-Many) in Spring Data JPA

how to select specific fields from the tables in laravel

How to summarize columns in joined tables?

How to select all joined rows from 2 tables including null

laravel 5 - Select all columns without asterix when joining tables with eloquent or query builder

Getting data of more than 2 joined tables in Laravel and Eloquent

Laravel Eloquent: How to select from multiple tables

MYSQL select max date from joined tables

How to alias a left joined table in laravel eloquent

How to select attribute from joined tables with aggregate function?

How do I access columns from joined tables using Linq, C# in asp.net MVC

how to use laravel eloquent to get and display data from other tables

Select max column from joined tables

Select count from joined tables

How to get fields from related tables with Eloquent PHP laravel?

Select multiple tables in Laravel Eloquent Relationship