Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

Don't explicitly state the select table when within a with().

E.G:

$data = Data::select(['id', 'name'])
	->where('id', $id)
	->with(['otherdata' => function($query)
	{
		//	Note that we also select() the belongsTo() relationship
		//	key (which is 'data_id' in this example)
		//	
		//	For a pivot, this will mean you'll have to select the id
		//	that you reference for the pivot (usually id)
		$query->select(['id', 'name', 'data_id', 'updated_at'])
			->orderBy('updated_at', 'desc')

		//	Note that we can actually have as many nested with calls
		//	that we desire
			->with(['innerdata' => function($query)
			{

				//	We are not limited to select(), we may treat $query
				//	as any other Eloquent query
				$query->select(['id', 'name', 'otherdata_id']);
			}]);
	}]);

return data;
Last updated 1 year ago.
0

can you confirm this still works ? (with last version of laravel 4.2) i tested this $d = User::select('id') ->where('id', 2) ->with(['photos' => function ($query) { $query->select(['id']); }]); return $d->get(); it only return User fields and photos will be empty array, but when i var_dump the $query->select(['id'])->get() , it gives me only the photos result , any will be be thanked.

Last updated 1 year ago.
0

I'm dealing with a similar issue as Ham3D. Var dump and logs show that I have the data available but it isn't attaching it to the return.

Last updated 1 year ago.
0

I'm going to try one more bump on this post. It appears lazy loading is broken in version 4.2, I'm not sure if this was on purpose or a bug. Just interested in knowing so I can plan around that and potentially implement my own lazy loading if it's a bug.

Doing for instance $dataStuff = DataStuff::with('relationshipOne', 'relationshipTwo')->get(['columnFromDataStuffOne'])

Returns empty arrays for both of the lazily loaded relationships.

Last updated 1 year ago.
0

I attempted to edit my post with my discovery, but I kept getting an error.

Sorry to kind of spam it up a bit but I felt it important to include this.

The ID field must be included in order for column selection to work.

http://www.laravel-tricks.com/tricks/column-selection-in-eager...

http://www.laravel-tricks.com/tricks/get-specific-columns-usin...

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

pdstefan pdstefan Joined 25 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.