1

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'];
}

1 Answers1

0

It should be like that :

//Controller
use App\Category;

...

    public function load()
        {
            return Category::all();
        }

Then, basically it's a GET route, so you don't need AJAX at the moment to test it. You can just introduce in browser localhost:8000/categories/load and should see the JSON result.

priMo-ex3m
  • 1,072
  • 2
  • 15
  • 29