Laravel 6

Argument 2 passed to Illuminate\Database\Eloquent\Builder::whereHas() must be an instance of Closure or null,

Avatar de MahmoudKhosravi
MahmoudKhosravi

I have two models which are related. I am trying to do a search in a report and only display the actual search results instead of all reports of the users in which the reports were found.

Controller

public function index(Request $request)
{
    $reports = Report::search($request->search)->paginate(30);
    if ($reports->total() == 0) {
        alert()->warning('no searched')->persistent('ok');
    }
    return view('report::admin.index', compact('reports'));
}

Report.php

public function scopeSearch($query, $term)
{
    return $query->when($term, function ($query, $term) {
        return $query->whereHas('plaintiff', 'like', "%{$term}%");
    });
}

public function plaintiff()
{
    return $this->belongsTo(User::class, 'plaintiff_id');
}

I see this error

Argument 2 passed to Illuminate\Database\Eloquent\Builder::whereHas() must be an instance of Closure or null,

Posté il y a 6 mois
Avatar de bestmomo
bestmomo

Hello,

As said in the error you must use a closure with whereHas, something like this :

$posts = Post::whereHas('comments', function (Builder $query) {
    $query->where('content', 'like', 'code%');
})->get();
Posté il y a 6 mois

Vous ne pouvez pas répondre à ce sujet.