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();
CodePudding user response:
Try create an alias to 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();
CodePudding user response:
$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