如何在Laravel中查询通过请求收到的字符串?

时间:2018-06-12 09:46:31

标签: php laravel

我想创建一个If-Statement来按名称搜索。

如果用户' John Doe'只想输入' joh'想要搜索他的名字,然后我该如何编写代码?

以下没有工作。

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();
        }
    ...

2 个答案:

答案 0 :(得分:2)

你可以这样做,

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

我希望这会对你有所帮助

答案 1 :(得分:0)

请参阅laravel文档https://laravel.com/docs/5.5/helpers#method-str-is

SELECT distinct c.reportingDate, c.clientId
FROM clients AS c
WHERE NOT EXISTS 
      ( SELECT 1
        FROM accounts_europe AS e
        WHERE e.reportingDate = c.reportingDate
          AND e.clientId = c.clientId

        union

        SELECT 1 -- line A. unexpected select error
        FROM accounts_usa AS u
        WHERE u.reportingDate = c.reportingDate
          AND u.clientId = c.clientId
      ) ;