0

I got the error in Laravel project, want to using DB method to call a database table. How to solve this?

Error: DataTables warning: table id=DataTables_Table_0 - Ajax error. For more information about this error, please see http://datatables.net/tn/7

Here is codes of controller

    public function index(Request $request)
{

    $title = 'Video Category List';

    if ($request->ajax()) {



        //$data = Category::where('type', 'Favourite')->get();
        $data = DB::table('action_status')
        ->where('type', 'Favourite')
        ->get(); 

        

        return Datatables::of($data)
            ->addIndexColumn()
            ->addColumn('name', function ($row) {
                return $row->name ?? '';
            })

            ->addColumn('image_path', function ($row) {
                return $row->image_path ?? '';
            })


            ->addColumn('status', function ($row) {
                if ($row->status == 1) {
                    return 'Active';
                } else {
                    return 'Inactive';
                }
            })

            ->addColumn('action', function ($row) {
                $btn = '<button type="button"   onclick="selectid2(' . $row->id . ')"
                class="btn btn-success waves-effect waves-light"style="margin-right:10px"   data-toggle="modal" data-target="#updatecategory">
                <i class="fa fa-edit"></i>

                </button> ';
                $btn2 = '<button type="button"   onclick="delete(' . $row->id . ')"
                 class="btn btn-danger waves-effect waves-light"style="margin-right:10px"   data-toggle="modal" data-target="#">
                 <i class="fa fa-trash"></i>
                 </button>';
                return  $btn . '' . $btn2;

                // <i class="bx bx-pencil  font-size-16 align-right "   ></i>
            })


            ->rawColumns(['action'])
            ->make(true);
    }

    return view('admin/videocategory/category', compact('title'));
}
4
  • you need to check network tab to get more information about the error Commented Nov 25, 2021 at 6:37
  • ok, I already go network tab, what I want to copy and paste here? Commented Nov 25, 2021 at 6:44
  • yeah or put screen shot Commented Nov 25, 2021 at 8:18
  • show your ajax request Commented Nov 25, 2021 at 11:45

1 Answer 1

1

You should start using Models in Laravel, and since you are using DataTables use jquery paired with ajax instead. Your approach is a bit complicated.

Also it is hard to debug the issue without other information from the network tab and a good test case scenario for this would be nice.

Sign up to request clarification or add additional context in comments.

4 Comments

from the network tab what information should I provide here, sorry I new in Laravel
could you send a screenshot of the network tab? Maybe which of your route/s has the public function index(Request $request) ?
Seeing your source code, I believe it has something to do with a column header mismatch. Meaning from your controller you added a column using the addColumn(), where the column header name does on exist nor matches to your html table headers.
Add some more information like links of docs for OP to understand properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.