Home > Enterprise >  Relationship is null in Datatables
Relationship is null in Datatables

Time:01-06

I receive this error in the DataTables: DataTables warning: table id=DataTables_Table_0 - Exception Message:

Attempt to read property "name" on null.

Although every position_id has the id from positions table so it shouldn't be null. If anyone would help, I will appreciate. My model:

 public function position()
    {
        return $this->belongsTo(Position::class);
    }

My controller:

class EmployeeController extends Controller
{
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $data = Employee::with('position')->select('id','name','email')->get();
            return Datatables::of($data)->addIndexColumn()
            ->addColumn('position', function (Employee $employee) {
                return $employee->position->name;
            })->addColumn('action', function($data){
                    $button = '<button type="button" name="edit" id="'.$data->id.'" > <i ></i>Edit</button>';
                    $button .= '   <button type="button" name="edit" id="'.$data->id.'" > <i ></i> Delete</button>';
                    return $button;
                })
                ->make(true);
        }

        return view('admin.employees.index');

    }
}

Script:

$(document).ready(function() {
        var table = $('.user_datatable').DataTable({
            processing: true,
            serverSide: true,
            ajax: "{{ route('admin.employees.index') }}",
            columns: [
                {data: 'id', name: 'id'},
                {data: 'name', name: 'name'},
                {data: 'email', name: 'email'},
                {data: 'position', name: 'position.name'},
                {data: 'action', name: 'action', orderable: false, searchable: false},
            ]
        });

Migration:

public function up()
    {
        Schema::create('employees', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->foreignId('position_id')->nullable();
            $table->string('phone_number');
            $table->date('recruitment_date');
            $table->string('email')->unique();
            $table->string('image_path')->nullable();
            $table->string('payment');
            $table->timestamps();
            $table->string('admin_created_id')->nullable();
            $table->string('admin_updated_id')->nullable();
        });
    }

CodePudding user response:

The problem was because of the select. Seems like if you're gonna use relationship, you have to add foreign key( which in my case is 'position_id') in your select or cut it completely and just use get. Thanks to all the people who helped in the comments.

CodePudding user response:

error in the DataTables: DataTables warning: table id=DataTables_Table_0 - Exception

this error mean when Datatable trying to fetch data from $data there is no collection or arrays that having position it's inside the get() method

{ collection[0]{
0 => 'Models/Employees'[0],
1 => 'Models/Employees'[1]
}

so, the return above doesn't have a position or attribute when you look as a $raw

from that point we will change the controller to be like that

public function index(Request $request)
    {
        if ($request->ajax()) {
            $data = Employee::query()->with('position')->select('id','name','email')->get();
            return Datatables::of($data)->addIndexColumn()
            ->addColumn('position', function (Employee $employee) {
                return $employee->position->name;
            })->addColumn('action', function($data){
                    $button = '<button type="button" name="edit" id="'.$data->id.'" > <i ></i>Edit</button>';
                    $button .= '   <button type="button" name="edit" id="'.$data->id.'" > <i ></i> Delete</button>';
                    return $button;
                })
                ->make(true);
        }

        return view('admin.employees.index');

    }

in case if not working you can try to change the db query but for that I need to dump the $data you can see the result into a Network section in Inspect element from your brawser dd($data);

  • Related