Laravel 5 simple ajax fetch record from database
How to get data using ajax? I have my ajax code that I have used in some of my projects when fetching records from the database, but dont know how to do it in laravel 5 because it has a route and controller.
I have this html
<select name="test" id="branchname">
<option value="" disabled selected>Select first branch</option>
<option value="1">branch1</option>
<option value="2">branch2</option>
<option value="3">branch3</option>
</select>
<select id="employees">
<!-- where the return data will be put it -->
</select>
and ajax
$("#branchname").change(function(){
$.ajax({
url: "employees",
type: "post",
data: { id : $(this).val() },
success: function(data){
$("#employees").html(data);
}
});
});
and in my controller i have declared 2 eloquent models, model 1 is for the branchname table and model 2 is for table employees
use App\branchname;
use App\employees;
so that I can get data like (see below)
public function getemployee($branch_no){
$employees = employees::where("branch_no", '=', $branch_no)->all();
return $employees;
}
How do I get back the records I pulled from the employee table? posting from routes where ajax first binds to the controller and returns a response to the ajax post request?
any help, suggestions, advice, ideas, hints would be highly appreciated. Thank!
PS: im new to Laravel 5.
source to share
First add the following entry to <head>
your section Master Layout
:
<meta name="csrf-token" content="{{ csrf_token() }}" />
This will add _token
to your view so you can use it for requests post and suchlike
, and then add the following code for the global ajax parameter to the shared file JavaScript
that is loaded on every request:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
So, you don't have to worry or add csrf_token
yourself for methods that require this _token
. Now for a post request, you can simply use the usual way to make a request Ajax
to Controller
with jQuery
, for example:
var data = { id : $(this).val() };
$.post(url, data, function(response){ // Shortcut for $.ajax({type: "post"})
// ...
});
Your route declaration for employees url
should match here url
, for example if you declared a route like this:
Route::post('employees/{branch_no}', 'EmployeeController@getemployee');
Then employees
is the response url
and returns json
to fill the element select
from yours Controller
, so the required code for that (including javaScript) is below:
$.post('/employees/'+$(this).val(), function(response){
if(response.success)
{
var branchName = $('#branchname').empty();
$.each(response.employees, function(i, employee){
$('<option/>', {
value:employee.id,
text:employee.title
}).appendTo(branchName);
})
}
}, 'json');
From Controller
should send data json_encoded
, for example:
public function getemployee($branch_no){
$employees = employees::where("branch_no", $branch_no)->lists('title', 'id');
return response()->json(['success' => true, 'employees' => $employees]);
}
Hope you have an idea.
source to share
First check the url of the page from which the ajax call is initiated
example.com/page-using ajax
In AJAX
If you call $.get('datalists', sendinput, function())
you are actually making a GET request
example.com/page-using ajax/datalists
In routes Route::get('page-using-ajax/datalists', xyzController@abc)
In controller method
if (Request::ajax())
{
$text = \Request::input('textkey');
$users = DB::table('datalists')->where('city', 'like', $text.'%')->take(10)->get();
$users = json_encode($users);
return $users;
}
In the Ajax success function
function(data) {
data = JSON.parse(data);
var html = "";
for (var i = 0; i < data.length; i++) {
html = html + "<option value='" + data[i].city + "'>";
};
$("#datalist-result").html(html);
}
source to share