skip to Main Content

I’m trying to transform a PostGreSQL Query in Laravel Query Builder Syntax, but isn’t working

SELECT users.*, rifs.rif
FROM users
JOIN rif_user on users.id = rif_user.user_id
JOIN rifs ON rifs.id = rif_user.rif_id
WHERE email = '[email protected]';


$busq = DB::table(['users','rifs'])
->select('rifs.nombre','users.email','rifs.rif')
->join('rif_user','users.id', '=', 'rif_user.user_id')
->join('rifs', 'rifs.id', '=', 'rif_user.rif_id')
->where('email' ,'[email protected]')
->get();

I Try the code on Laravel Tinker and there is the Output:

->select(‘rifs.nombre’,’users.email’,’rifs.rif’)
PHP Parse error: Syntax error, unexpected T_OBJECT_OPERATOR on line 1
->join(‘rif_user’,’users.id’, ‘=’, ‘rif_user.user_id’)
PHP Parse error: Syntax error, unexpected T_OBJECT_OPERATOR on line 1
->join(‘rifs’, ‘rifs.id’, ‘=’, ‘rif_user.rif_id’)
PHP Parse error: Syntax error, unexpected T_OBJECT_OPERATOR on line 1
->where(’email’ ,’[email protected]’)
PHP Parse error: Syntax error, unexpected T_OBJECT_OPERATOR on line 1
->get();

2

Answers


  1. Chosen as BEST ANSWER

    $busq = DB::table('users','rifs')->select('rifs.nombre','users.email','rifs.rif')->join('rif_user','users.id', '=', 'rif_user.user_id')->join('rifs', 'rifs.id', '=', 'rif_user.rif_id')->where('email' ,'[email protected]')->get();

    i done it puting all code in a single line


  2. Try creating an alias for each table, like this

    $busq = DB::table('users as u')
    ->select('r.nombre','u.email','r.rif')
    ->join('rif_user as ru','u.id', '=', 'ru.user_id')
    ->join('rifs as r', 'r.id', '=', 'ru.rif_id')
    ->where('u.email' ,'[email protected]')
    ->get();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search