简体   繁体   中英

Laravel - Eloquent where with relationship?

I have the following code:

return Technology::WhereIn('uuid', $technologies)->get();

$technologies is an array of UUIDs and in my $technologies table I have a uuid field.

So this will return the technologies that match up with the UUIDs, but I also have an equipment_id in my technologies table and my end game goal is to get a list of equipment id's that belong to the technologies that match the UUIDs in my $technologies variable.

I could do a foreach loop and add the equipment id's to an array but I was wondering if there was a way to do some eloquent magic that would include the equipment relationship into the query.

Something like this: (this doesn't work)

return Technology::WhereIn('uuid', $technologies)->Equipment->get();

I think that was called "Has Many Through" Relationship.

I answered a same question at:

How to fetch data by using Laravel Eloquent Relatinoships

如果只是一组设备ID,则可以在模型上使用lists()方法,如下所示:

return Technology::whereIn('uuid', $technologies)->lists('equipment_id');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM