简体   繁体   中英

ErrorException: Undefined variable: request in file

Error ErrorException: Undefined variable: request in file always appears, even though I have defined it as below

public function postlog_show(Request $request)
    {
        $log = $request->log;
        $userlocation = Auth::user()->location;
        $postlocations = Postlocation::orderBy('id', 'DESC')->where('location', $userlocation)
                ->whereHas('postlognya', function (Builder $query) {
                    $query->where('log', 'like', '%' . $request->log . '%');
                })
        ->get();

        return view('front.postlog2')
            ->with('postlocations', $postlocations)
        ;
    }

you will have to use $request variable in whereHas function, like I've shown

public function postlog_show(Request $request)
{
    $log = $request->log;
    $userlocation = Auth::user()->location;
    $postlocations = Postlocation::orderBy('id', 'DESC')->where('location', $userlocation)
            ->whereHas('postlognya', function (Builder $query) use($request){
                $query->where('log', 'like', '%' . $request->log . '%');
            })
    ->get();

    return view('front.postlog2')
        ->with('postlocations', $postlocations)
    ;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM