jquery - Laravel 5 how to convert AJAX response JSON into PHP, so I can loop through it with foreach on the view? -


i'm working on ajax search function users table, , i've managed until content of response json file. can't figure out how hand it's content on $users php array, loop through it.

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 " + data + ".");             }         });     } }); 

the route: route::get('results', 'searchescontroller@search');

the search function in 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 $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, content of \response::json($users); should end on view $users. or should change whole approach , find solution console.log js?

thanks in advance!

change

return \response::json($users); 

to

return view('results', array( 'users' => $users )); 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -