php - building search query in laravel5 gives error Syntax error or access violation: 1064 -
i want implement simple search functionality in laravel5 app
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 charactres"); } else{ $users = user::where(db::raw("concat (first_name, ' ', last_name), "), "like", "%{$query}%") ->orwhere('username', "like", "%{$query}%") ->get(); dd('search ok' . $users); } return view('search.results'); } }
with code provided above error
queryexception in connection.php line 673: sqlstate[42000]: syntax error or access violation: 1064 have error in sql syntax; check manual corresponds mariadb server version right syntax use near ' ? or `username` ?' @ line 1 (sql: select * `users` concat (fisrt_name, ' ', last_name), %s% or `username` %s%) in connection.php line 673 @ connection->runquerycallback('select * `users` concat (fisrt_name, ' ', last_name), ? or `username` ?', array('%{$query}%', '%{$query}%'), object(closure)) in connection.php line 629 @ connection->run('select * `users` concat (fisrt_name, ' ', last_name), ? or `username` ?', array('%{$query}%', '%{$query}%'), object(closure)) in connection.php line 342 @ connection->select('select * `users` concat (fisrt_name, ' ', last_name), ? or `username` ?', array('%{$query}%', '%{$query}%'), true) in builder.php line 1508 @ builder->runselect() in builder.php line 1494 @ builder->get(array('*')) in builder.php line 596 @ builder->getmodels(array('*')) in builder.php line 303 @ builder->get() in searchcontroller.php line 25 @ searchcontroller->getresults(object(request)) @ call_user_func_array(array(object(searchcontroller), 'getresults'), array(object(request))) in controller.php line 80 @ controller->callaction('getresults', array(object(request))) in controllerdispatcher.php line 146 @ controllerdispatcher->call(object(searchcontroller), object(route), 'getresults') in controllerdispatcher.php line 94 @ controllerdispatcher->illuminate\routing\{closure}(object(request)) @ call_user_func(object(closure), object(request)) in pipeline.php line 52 @ pipeline->illuminate\routing\{closure}(object(request)) @ call_user_func(object(closure), object(request)) in pipeline.php line 103 @ pipeline->then(object(closure)) in controllerdispatcher.php line 96 @ controllerdispatcher->callwithinstack(object(searchcontroller), object(route), object(request), 'getresults') in controllerdispatcher.php line 54
first check $query contain string search
user::where(db::raw('concat(first_name," ", last_name)'), 'like', '%'.$query.'%')->orwhere('username', 'like', '%'.$query.'%')->get();
Comments
Post a Comment