Laravel wrapping int parameters with backtick

  laravel, laravel-query-builder

In my Laravel query builder, I have this part:

 ->leftJoin('tbluserprivileges', function ($join) use ($id) {
            $join->on('', '=', 'tbluserprivileges.PrivilegeID');
            $join->on('tbluserprivileges.UserID', '=', (int)$id);

I wanted to have two conditions on a LEFT JOIN with the other parameters to be dynamic, which is the user ID. However the SQL output is

select `tblprivileges`.`id`, `tblprivileges`.`Description`, `tbluserprivileges`.`UserID` from `tblprivileges` left join `tbluserprivileges` on `tblprivileges`.`id` = `tbluserprivileges`.`PrivilegeID` and `tbluserprivileges`.`UserID` = `1`

As you can see, the bit where:

left join `tbluserprivileges` on `tblprivileges`.`id` = `tbluserprivileges`.`PrivilegeID` and `tbluserprivileges`.`UserID` = `1`

The 1 is being wrapped in backticks ( ` ), therefore it is being read as a column when it isn’t. I tried to run the code outside of Laravel and removed the wrapping backticks and everything works fine. I am quite unsure on how to remove the backticks, I tried casting it as an integer but it still doesn’t work.

Source: Laravel

Leave a Reply