首頁  >  問答  >  主體

資料表中的關係為空

我在 DataTables 中收到此錯誤:DataTables 警告:表 id=DataTables_Table_0 - 異常訊息:

嘗試讀取 null 屬性「name」。

雖然每個position_id都有位置表中的id,所以它不應該為空。如果有人願意提供協助,我將不勝感激。我的模型:

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

我的控制器:

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.'" class="edit btn btn-primary btn-sm"> <i class="bi bi-pencil-square"></i>Edit</button>';
                    $button .= '   <button type="button" name="edit" id="'.$data->id.'" class="delete btn btn-danger btn-sm"> <i class="bi bi-backspace-reverse-fill"></i> Delete</button>';
                    return $button;
                })
                ->make(true);
        }

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

    }
}

腳本:

$(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},
            ]
        });

遷移:

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();
        });
    }

P粉704196697P粉704196697178 天前266

全部回覆(2)我來回復

  • P粉419164700

    P粉4191647002024-03-29 16:57:08

    此錯誤表示當 Datatable 嘗試從 $data 取得資料時,沒有集合或陣列將其定位在 get() 方法內

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

    所以,當您查看 $raw 時,上面的返回沒有位置或屬性

    從那時起,我們將把控制器更改為這樣

    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 .= '   ';
                        return $button;
                    })
                    ->make(true);
            }
    
            return view('admin.employees.index');
    
        }

    如果不起作用,您可以嘗試更改資料庫查詢 但為此我需要轉儲 $data 您可以在瀏覽器中的 Inspect 元素的 Network 部分中看到結果 dd($data);

    #

    回覆
    0
  • P粉156415696

    P粉1564156962024-03-29 13:49:48

    問題是因為選擇。似乎如果你要使用關係,你必須在選擇中添加外鍵(在我的例子中是“position_id”),或者完全刪除它並只使用 get 。感謝所有在評論中提供幫助的人。

    回覆
    0
  • 取消回覆