简体   繁体   中英

How can I query a string received with a request in Laravel?

I want to create an If-Statement for searching by name.

If the user 'John Doe' wants to search for his name by just typing 'joh', then how do I write the code?

Below didn't worked.

public function search_by_name(Request $request) 
    { 
        if('%'.$request->name.'%' != Auth::user()->name) {
            Flash::error('Sorry, you are not Auth::user()->name');
            return redirect()->back();
        }
    ...

You can do this like,

   if (strpos($request->name, Auth::user()->name) !== false) {
        Flash::error('Sorry, you are not Auth::user()->name');
            return redirect()->back();
    }

I hope this will help you

See laravel documentation https://laravel.com/docs/5.5/helpers#method-str-is

if (!str_is('*' . $request->name . '*', Auth::user()->name)) {
    Flash::error('Sorry, you are not Auth::user()->name');    
    return redirect()->back();
}

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