Sunday, April 16, 2017

Laravel Query builder : left join, select, and where not working

Please help, I'm in trouble, I'm in deadline but the Laravel query builder seems not in a good mood!.

Btw, Here is my table structure :

Table user Structure

ID, USERNAME, NAME, USER_ROLE_MODULE_ID

Table user_role_module Structure

ID, NAME 

And, this is my query builder statement

DB::table('user')
->leftJoin('user_role_module', 'user.id', '=', 'user_role_module.user_id')
->select('user.*', 'user_role_module.name as user_role')
->where("name LIKE '%Jhon%' ") "or other example" ->where("user_role LIKE '%admin%' ")
->paginate(10);

My Goal :

I just want to get all data from user + user role name from user_role_module AND also applies in WHERE statement so I can use it for search feature.

The problem is :

  1. if I search for name, it return error ambiguous column //Laravel is confusing whether taking name from user table or user_role_module table!

  2. if I search for user_role, then the column doesn't exist

Why is it? What is the solution?

Sorry for long question, any answer will highly appreciated!



via Angger

Advertisement