Laravel yajrabox – how to add a column which render a blade file?

  laravel, yajra-datatable

I’ve been studying this yajrabox tutorial https://www.tutsmake.com/laravel-8-ajax-crud-using-datatable-tutorial/

how the author add a new column for ‘company-action’, which it is a blade file.

public function index()
{
    if(request()->ajax()) {
        return datatables()->of(Company::select('*'))
        ->addColumn('action', 'company-action')
        ->rawColumns(['action'])
        ->addIndexColumn()
        ->make(true);
    }
    return view('companies');
}

but this is a defective codes, as he just added a new column for string ‘company-action’.

I can make this work by including the html codes in Controller, but that would make the Controller bloated.

public function index()
{
    if(request()->ajax()) {
        return datatables()->of(Company::select('*'))
        ->addColumn('action', '
        <a href="javascript:void(0)" data-toggle="tooltip" onClick="editFunc({{ $id }})" data-original-title="Edit"
            class="edit btn btn-success edit">Edit</a>
        <a href="javascript:void(0);" id="delete-compnay" onClick="deleteFunc({{ $id }})" data-toggle="tooltip"
            data-original-title="Delete" class="delete btn btn-danger">Delete
        </a>
        ')
        ->rawColumns(['action'])
        ->addIndexColumn()
        ->make(true);
    }
    return view('company.company-index');
}

how do I add a column for this blade, company-action.blade.php?

<a href="javascript:void(0)" data-toggle="tooltip" onClick="editFunc({{ $id }})" data-original-title="Edit" class="edit btn btn-success edit">
Edit
</a>
<a href="javascript:void(0);" id="delete-compnay" onClick="deleteFunc({{ $id }})" data-toggle="tooltip" data-original-title="Delete" class="delete btn btn-danger">
Delete
</a>

Source: Laravel

Leave a Reply