I want to load the data into the table at the start of the page and after the operations such as create, edit and delete using JQUERY AJAX.
I have CategoryController. In there I have function load() to return JSON data to the view. In blade file, I have ajax function that add new category to the database and it works. What I am getting trouble is loading data from database.
Controller
public function load()
{
$categories = Category::all();
return response()->json($categories);
}
Route
Route::get('/categories/load', 'CategoryController@load');
JQUERY
function loadCategories() {
$.ajax({
url: "/categories/load",
type: 'GET',
success: function(data){
console.log(data)
}
})
}
Category Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = ['name'];
}