Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

Hi, i am on my phone so i cant go into Detail, but you should look up "Working with pivots" and Eloquent relationships in the laravel docs.

I think you will find what you are looking for.

Good luck.

Last updated 2 years ago.
0

Sure you can:

$item = new Item;

$items = $item->whereIn($item->category()->getForeignKey(), function($query) use ($user) {
    $query->select($user->categories()->getOtherKey())
       ->from($user->categories()->getTable())
       ->where($user->categories()->getForeignKey(), '=', $user->getKey());
})->orderBy('title')->get();
Last updated 2 years ago.
0

Ah, exactly what I was looking for, thank you!

I did read the documentation but it didn't specify how to get the local/foreign key from what I could find, and I guess I was just bad at reading the API docs. Feels a lot better to not have to mix hard-coded column and table names, kind of defeats a large part of Eloquents purpose :). I'll try it out.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

ErikEklund erikeklund Joined 14 Jun 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.