I'm working on an AJAX search function for my users
table, and I've managed to get until I get back the content of the response JSON file. But I can't figure out how could I hand it's content over to the $users
PHP array, so I could loop through it.
我正在为我的用户表开发一个AJAX搜索功能,并且我已经设法获取,直到我找回响应JSON文件的内容。但我无法弄清楚我怎么能把它的内容交给$ users PHP数组,所以我可以循环它。
The JS:
$('.searchbar').keypress(function (e) {
if (e.which == 13) {
$.ajax({
dataType: "json",
url: '/results',
data: {keyword: $('.searchbar').value()},
success: function (result) {
console.log(result);
},
error: function(){
console.log("No results for " + data + ".");
}
});
}
});
The route: Route::get('results', 'SearchesController@search');
路线:Route :: get('results','SearchesController @ search');
The search function in SearchesController
:
SearchesController中的搜索功能:
public function search(Request $request) {
$keyword = $request->get('keyword');
$users = \App\User::where("username", "LIKE","%$keyword%")
->orWhere("firstname", "LIKE", "%$keyword%")
->orWhere("lastname", "LIKE", "%$keyword%")
->orWhere("email", "LIKE", "%$keyword%")
->orWhere("phone", "LIKE", "%$keyword%")->get();
return \Response::json($users);
}
The view:
@foreach($users as $user)
<tr>
<td class='text-center'>{{$user->username}}</td>
<td class='text-center'>{{$user->firstname}}</td>
<td class='text-center'>{{$user->lastname}}</td>
<td class='text-center'>{{$user->email}}</td>
<td class='text-center'>{{$user->phone}}</td>
<td class='text-center'>{!! link_to_action('UsersController@edit', 'Edit', $user->id, ['class'=>'btn btn-warning', 'style'=>'margin-bottom:5px']); !!}
{!! Form::open(['action' => ['UsersController@destroy', $user->id], 'method' => 'DELETE']) !!}
{!! Form::submit('Delete', ['class'=>'btn btn-danger']) !!}
{!! Form::close() !!}</td>
</tr>
@endforeach
So shortly, the content of \Response::json($users);
should end up on the view as $users
. Or should I change my whole approach and find a solution to console.log
everything with JS?
所以很快,\ Response :: json($ users)的内容;应该以$ users结束视图。或者我应该改变整个方法并使用JS找到console.log的解决方案吗?
Thanks in advance!
提前致谢!
2 个解决方案
#1
1
change
return \Response::json($users);
to
return view('results', array( 'users' => $users ));
#2
0
Try this:
Controller
$users = \App\User::where("username", "LIKE","%$keyword%")
->orWhere("firstname", "LIKE", "%$keyword%")
->orWhere("lastname", "LIKE", "%$keyword%")
->orWhere("email", "LIKE", "%$keyword%")
->orWhere("phone", "LIKE", "%$keyword%")->get();
if($users)
return response()->json(['users' => $users], 200);
return response()->json(['msg' => 'No result found!'], 404);
Ajax
success: function (data) {
$.each(data.users, function(i, user){
//do something
consloe.log(user.firstname);
});
},
error: function(error){
console.log(error.msg);
}
#1
1
change
return \Response::json($users);
to
return view('results', array( 'users' => $users ));
#2
0
Try this:
Controller
$users = \App\User::where("username", "LIKE","%$keyword%")
->orWhere("firstname", "LIKE", "%$keyword%")
->orWhere("lastname", "LIKE", "%$keyword%")
->orWhere("email", "LIKE", "%$keyword%")
->orWhere("phone", "LIKE", "%$keyword%")->get();
if($users)
return response()->json(['users' => $users], 200);
return response()->json(['msg' => 'No result found!'], 404);
Ajax
success: function (data) {
$.each(data.users, function(i, user){
//do something
consloe.log(user.firstname);
});
},
error: function(error){
console.log(error.msg);
}