Transform Brute Inner Join to Laravel Join

Asked

Viewed 284 times

0

I’m updating a system that is developed in pure PHP and I’m putting it in Laravel, however, I’ve never made more than a Join and with "AND" parameters so I would like you to help me, how can I put this SELECT in the Laravel controller?

<?php 
  $users = "SELECT * FROM ma_usuario u, ma_user_tipo t, ma_user_tipo_rel r 
            WHERE ma_user_tipo.tp_usr_id=ma_user_tipo_rel.tp_usr_id 
            AND ma_usuario.usr_id=ma_user_tipo_rel.usr_id 
            AND ma_usuario.usr_status='Ativo' 
            AND r.tp_usr_id='$id' 
            ORDER BY u.usr_id";
 ?>

As are several parameters I had a knot in the brain and did not understand how to assemble.

1 answer

0


Following the documentation (https://laravel.com/docs/5.5/queries#joins) you can do your query this way:

DB::table('ma_usuario')
    ->join('ma_user_tipo_rel' 'ma_usuario.usr_id', '=', 'ma_user_tipo_rel.usr_id')
    ->where([
        ['ma_usuario.usr_status', '=', 'Ativo'],
        ['ma_user_tipo_rel.tp_usr_id', '=', $id],
    ])
    ->orderBy('ma_usuario.usr_id');
  • Thank you!! I read and reread the documentation and was not understanding how I applied Where.

  • You’re welcome. You can use two separate Where’s as well, I ended up putting the two together for practicality.

Browser other questions tagged

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