How to use Inner Join in Laravel 5?

Asked

Viewed 10,458 times

3

I have the following loop in a view table

@foreach ($filme as $f)
    <tr>
        <td>{{ $f->fil_id }}</td>
        <td>{{ $f->fil_filme }}</td>
        <td>{{ $f->fil_sinopse }}</td>
        <td>{{ $f->fil_lancamento }}</td>        
        <td>{{ $f->cat_id }}</td>
    </tr>
@endforeach

But it only displays the category id as it is in the movie table. How to give an INNER JOIN with the category table in Laravel 5?

  • Place models and controller?

  • Puts the code that generates the variable $filme

1 answer

4


You can use Query Builder to make joins

To run a basic "merge", you can use the method in a query constructor instance. The first argument passed to the method join is the name of the table to which you need to join, while the other arguments specify the column constraints for the join. Of course, as you can see, you can join several tables in a single query:

$users = DB::table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.*', 'contacts.phone', 'orders.price')
            ->get();

LARAVEL - Database: Query builder

  • In this case, I would put $f->cat_id.cat_description if I used this Query Builder?

  • 1

    I got it. Thank you :)

  • That’s all for now. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.