0
votes

My query is like this :

$search = $request->get('search');
$search_date = $request->get('search_date');
$store = Store::whereDate('updated_at', $search_date)->orWhereHas('user'), function($query) use ($search) {
    $query->where(function ($q) use($search)
    {
        $q->where('name', 'LIKE', '%',$search.'%');
    });
})->paginate(10);

When executed, there is no error. But the result not match with the filter date

When I only use wheredate without wherehas, it's match with the filter

What the wheredate and wherehas is not can run simultaneously?

Update

I have 2 input text, that is input text for datepicker and input text to search name. When the user wants to do searching, he can input updated date and input name. Then click the search button. And the system will filter it out.

1
First of all you're missing a > before paginate and a ; at the end of the second rowCalin Blaga
If you need and conjunctions, don't use or.Alex Blex
@CalinBlaga Thx, fixed.rap-2-h
I had update my questionsamuel toh

1 Answers

0
votes

You used orWhereHas. If you want both tour criteria (wheredate AND wherehas), you should use wherehas instead of orWhereHas:

$store = Store::whereDate('updated_at', $search_date)->whereHas('user'), function($query) use ($search) {
    $query->where(function ($q) use($search) {
        $q->where('name', 'LIKE', '%',$search.'%');
    });
})->paginate(10);

In other words, if you need both condition to be true, use and, not or (and is implicit with laravel query builder)