I have a ModelA with columns: name, last_name, email
I want ModelA with the name Test to add an extra where on email
what I tried is the following
$model_a_res = A::when(
function($query){
return $query->where('name','Test');
},
function ($query) {
$query->where('email', 'NOT LIKE', '%@test%');})
->first();
I want this second where to only run on A with column name = Test however, this is not working.
I tried it with user name Mike and his email mike@test.com and he didn't appear in the result.
some data
id name email
1 Test test@test.com
2 Test something@hotmail.com
3 Mike mike@test.com
4 Mike mike@hotmail.com
both 3 and 4 should be in the results 1 and 2 since they have name = Test will have another query which will check if he does have @test in his email if he does he will not be returned returned Rows are 2, 3, 4
You can't use when()
for this.
$model_a_res = A::where(function ($query) {
$query->where('name', '!=', 'Test')
->orWhere(function ($query) {
$query->where('name', '=', 'Test')
->where('email', 'NOT LIKE', '%@test%');
});
})->get();
Outputs
1 Test test@test.com - will skipp
2 Test something@hotmail.com - added
3 Mike mike@test.com - added
4 Mike mike@hotmail.com - added