Laravel ajax fetch data

Laravel ajax fetch data

Friends, just like we run crud operations in Laravel, with the help of which we insert, delete, fetch and update data in the database, in today’s video we will run page operations, that too with the help of the pager and display our data on our front end with the help of ajax.

We will search the data from the database and show it in a table and inside this table we will run the cred operation with the help of Ajax.

A. view.blade.php

<script>

$(document).ready(function(){

let fetch_bag = "{{route('fetch_bags')}}";

$.ajax({

type: 'POST',
url: fetch_bag,
data: {_token: '{{csrf_token()}}'},
success: function(data){
$('#records').html(data);
}

});

});

</script>


B. web.php

Route::post('fetch-bags', 'ShipmentController@fetch_bags')->name('fetch_bags');

 

C. Controller.php

function fetch_bags(Request $req){

$data = DB::table('bags')->get();
$count = DB::table('bags')->count('id');

if($count){

foreach($data as $d){

$table = "<tr>
<td>$d->title</td>
<td>$d->address</td>
<td><a href='edit-bags/$d->id'><button class='btn btn-success'>Edit</button></a> &nbsp;
<a href='delete-bags/$d->id'><button class='btn btn-danger'>Delete</button></a></td>
</tr>";

echo $table;

}

}

else{
echo "<h3>Data not found !</h3>";
}

}

Leave a Reply