Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在laravel5中构建搜索查询时出现错误语法错误或访问冲突:1064_Php_Laravel 5_Mariadb - Fatal编程技术网

Php 在laravel5中构建搜索查询时出现错误语法错误或访问冲突:1064

Php 在laravel5中构建搜索查询时出现错误语法错误或访问冲突:1064,php,laravel-5,mariadb,Php,Laravel 5,Mariadb,我想在laravel5应用程序中实现一个简单的搜索功能 use DB; use App\Models\User; use Illuminate\Http\Request; use ConnectIn\Http\Requests; class SearchController extends Controller { public function getResults(Request $request) { $query = $request->inp

我想在laravel5应用程序中实现一个简单的搜索功能

use DB;
use App\Models\User;
use Illuminate\Http\Request;

use ConnectIn\Http\Requests;

class SearchController extends Controller
{
    public function getResults(Request $request)
    {

        $query = $request->input('name');
        if (!$query) {
            return  redirect()-> route('home') -> with('info', "Search must contain some charactres");
        }
        else{

        $users = User::where(DB::raw("CONCAT (first_name, ' ', last_name), "),
        "LIKE", "%{$query}%")
    ->orwhere('username', "LIKE", "%{$query}%")
    ->get();
    dd('search is ALL OK' . $users);
    }
        return view('search.results');
    }

}
使用上面提供的代码,我得到了一个错误

    QueryException in Connection.php line 673:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' LIKE ? or `username` LIKE ?' at line 1 (SQL: select * from `users` where CONCAT (fisrt_name, ' ', last_name), LIKE %s% or `username` LIKE %s%)
    in Connection.php line 673
    at Connection->runQueryCallback('select * from `users` where CONCAT (fisrt_name, ' ', last_name), LIKE ? or `username` LIKE ?', array('%{$query}%', '%{$query}%'), object(Closure)) in Connection.php line 629
    at Connection->run('select * from `users` where CONCAT (fisrt_name, ' ', last_name), LIKE ? or `username` LIKE ?', array('%{$query}%', '%{$query}%'), object(Closure)) in Connection.php line 342
    at Connection->select('select * from `users` where CONCAT (fisrt_name, ' ', last_name), LIKE ? or `username` LIKE ?', array('%{$query}%', '%{$query}%'), true) in Builder.php line 1508
    at Builder->runSelect() in Builder.php line 1494
    at Builder->get(array('*')) in Builder.php line 596
    at Builder->getModels(array('*')) in Builder.php line 303
    at Builder->get() in SearchController.php line 25
    at SearchController->getResults(object(Request))
    at call_user_func_array(array(object(SearchController), 'getResults'), array(object(Request))) in Controller.php line 80
    at Controller->callAction('getResults', array(object(Request))) in ControllerDispatcher.php line 146
    at ControllerDispatcher->call(object(SearchController), object(Route), 'getResults') in ControllerDispatcher.php line 94
    at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
    at Pipeline->Illuminate\Routing\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
    at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96
    at ControllerDispatcher->callWithinStack(object(SearchController), object(Route), object(Request), 'getResults') in ControllerDispatcher.php line 54

您的查询有语法错误和输入错误

CONCAT (fisrt_name, ' ', last_name), LIKE %s%
应该是:

CONCAT (first_name, ' ', last_name) LIKE %s%
您还可以执行以下操作:

SELECT * FROM `users`
CONCAT (first_name, ' ', last_name) AS fullname
WHERE `username` LIKE %s%
HAVING fullname LIKE %s%

您的查询有语法错误和输入错误

CONCAT (fisrt_name, ' ', last_name), LIKE %s%
应该是:

CONCAT (first_name, ' ', last_name) LIKE %s%
您还可以执行以下操作:

SELECT * FROM `users`
CONCAT (first_name, ' ', last_name) AS fullname
WHERE `username` LIKE %s%
HAVING fullname LIKE %s%

首先检查$query是否包含要搜索的字符串

User::where(DB::raw('CONCAT(first_name," ", last_name)'), 'like', '%'.$query.'%')->orwhere('username', 'like', '%'.$query.'%')->get();

首先检查$query是否包含要搜索的字符串

User::where(DB::raw('CONCAT(first_name," ", last_name)'), 'like', '%'.$query.'%')->orwhere('username', 'like', '%'.$query.'%')->get();

好的,可以,但是请把“first_name”改为“first_name”,顺便说一句,谢谢!请将问题中的第一个名字改为第一个名字。。我只是复制了它。对不起,好的,但请将“first_name”改为“first_name”,顺便说一句,谢谢!请将问题中的第一个名字改为第一个名字。。我只是复制了它。很抱歉