I get two parameters in my function as mobile
and tel
.
If they aren’t null, I want to search between my customers where it has a relation with my lead.
This is my query:
$lead = $this->query()
->with([
'customer' => function ($query) use ($mobile, $tel) {
$query->when($mobile != '', function ($query) use ($mobile) {
return $query->where('mobile','=', $mobile);
});
$query->when($tel != '', function ($query) use ($tel) {
return $query->where('tel','=', $tel);
});
},
])
->first();
However, it returns the wrong result.
2
Answers
You can use
if
statements to check if$mobile
and$tel
are not null and then directly apply the where clauses to the $query.