R
R
Ruslan Absalyamov2018-03-17 22:18:18
Laravel
Ruslan Absalyamov, 2018-03-17 22:18:18

How to shorten the code for a many-to-many relationship?

Maybe I read the documentation incorrectly, but the many-to-many relationship works with find (), But if I need to print out the entire value whose id_user is equal to the authorized user. That's where I got a problem. Is it possible for me to shorten the code?

$directions = Direction::where('id_user', Auth::user()->id)->with('executes');
        $execute = [];
        foreach ($directions->get() as $value){
            foreach (Direction::find($value->id)->executes()->get() as $item){
                $execute[] = $item->name;
            }
        }
        dd($execute);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
anlamas, 2018-03-18
@rusline18

Of course it's wrong. I don't know where you found such an example in the documentation

// вижу там такой пример
$users = User::with('podcasts')->get();
// твой код сокращается до
$directions = Direction::where('id_user', Auth::user()->id)->with('executes')->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question