I have a users table where the clients can either register themselves or call the company to register them in case the process is complicated for them, i wanted to organize the clients list in order to have: the self registered clients in one page ,and the clients registered by an employee in another page, my application is using Laravel/MySQL, but the query was too complicated for me to do it by myself
Here is what I have tried using eloquent:
$clients = User::where([['role','Client'],['created_by','id']])->get();
This query is meant to select rows that have been created by the same person in another words: if I registered myself through the form my row will look like this :
id: 1
created_by:1
but this query didn't work.
I could select all the rows and do an if test for each row in my view but that's not optimal and seem like cheating, I want to be able to understand the perfect solution in MySQL. In order to develop my skills.
CodePudding user response:
You can use whereColumn, which is describe in the Additional Where Clauses section in laravels documentation.
And for the role you use a normal where.
$clients = User::where('role', 'Client')->whereColumn('created_by', 'id')->get();
With your code you're searching for a User where created_by has the value id.
CodePudding user response:
try use this
$clients = User::where('role','Client')->where('created_by','id')->get();
