Laravel - Yajra 数据表异常消息:尝试获取非 object 的 属性

Laravel - Yajra Datatables Exception Message: Trying to get property of non object

我是laravel的初学者。我想加入一些 tables 并让它显示在 datatables 中,我正在使用 yajra datatables。我有四个 table,分别是 Schedule、Trip、BusTransport 和 BusConductor。我现在面临的问题是我想在 blade 视图中显示时间表,我想从 Trip Tablebus_id 来自 BusTransport Tablebus_conductor_id 来自 BusConductor Table。我能够显示 Trip 和 BusConductor 的数据,但我不断收到来自 BusTransport 的其中一个字段的错误。它向我显示 DataTables 警告:table id=example - 异常消息:尝试获取非 object 的 属性 'bus_code' 。谁能帮我发现我的问题?谢谢和感激。

旅行Table

    public function up()
    {
        Schema::create('trip', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->bigIncrements('id');
            $table->string('trip_code')->unique();
            $table->string('trip_origin');
            $table->string('trip_destination');
            $table->date('depart_date');
            $table->time('depart_time');
            $table->timestamps();
        });
    }

公交车Table

    public function up()
    {
        Schema::create('bus_transport', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->bigIncrements('id');
            $table->string('bus_code')->unique();
            $table->string('bus_number_plate');
            $table->timestamps();
        });
    }

BusConductor Table

    public function up()
    {
        Schema::create('bus_conductor', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->bigIncrements('id');
            $table->string('bus_conductor_name');
            $table->string('bus_conductor_contact_number');
            $table->timestamps();
        })

日程安排Table

    public function up()
    {
        Schema::create('schedule', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->bigIncrements('id');
            $table->string('schedule_code')->unique();
            $table->bigInteger('trip_id')->unsigned();
            $table->bigInteger('bus_id')->unsigned();
            $table->bigInteger('bus_conductor_id')->unsigned();
            $table->timestamps();
        });

        Schema::table('schedule', function (Blueprint $table) {
            $table->foreign('trip_id')->references('id')->on('trip')->onUpdate('cascade');
            $table->foreign('bus_id')->references('id')->on('bus_transport')->onUpdate('cascade');
            $table->foreign('bus_conductor_id')->references('id')->on('bus_conductor')->onUpdate('cascade');
        });
    }

行程模型

class Trip extends Model
{
    use HasFactory;

    protected $table = 'trip';
    protected $fillable = [
        'trip_code',
        'trip_origin',
        'trip_destination',
        'depart_date',
        'depart_time'
    ];

    public function schedule()
    {
        return $this->hasMany(Schedule::class);
    }
}

公交模型

class BusTransport extends Model
{
    use HasFactory;

    protected $table = 'bus_transport';
    protected $fillable = [
        'bus_code',
        'bus_number_plate'
    ];

    public function schedule()
    {
        return $this->hasMany(Schedule::class);
    }


}

BusConductor 模型

class BusConductor extends Model
{
    use HasFactory;

    protected $table = 'bus_conductor';
    protected $fillable = [
        'bus_conductor_name',
        'bus_conductor_contact_number'
    ];

    public function schedule()
    {
        return $this->hasMany(Schedule::class);
    }
}

排程模型

class Schedule extends Model
{
    use HasFactory;
    protected $table = 'schedule';
    protected $fillable = [
        'schedule_code',
        'trip_id',
        'bus_id',
        'bus_conductor_id'
    ];

    public function trip(){
        return $this->belongsTo(Trip::class);
      }

      public function busTransport(){
        return $this->belongsTo(BusTransport::class);
      }

    public function busConductor()
    {
        return $this->belongsTo(BusConductor::class);
    }

}

Ajax 请求时间表数据tables

<script type="text/javascript">
    $(function() {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        var table = $('.table-striped.first').DataTable({
            processing: true,
            serverSide: true,
            ajax: "{{ route('schedule.list') }}",
            columns: [{
                    data: 'schedule_code',
                    name: 'schedule_code'
                },
                {
                    data: 'trip_code',
                    name: 'trip_code'
                },
                {
                    data: 'bus_code',
                    name: 'bus_code'
                },
                {
                    data: 'bus_conductor_name',
                    name: 'bus_conductor_name'
                },
                {
                    data: 'action',
                    name: 'action',
                    orderable: true,
                    searchable: true
                },
            ]
        });

调度控制器

    public function schedule(Request $request)
    {
        if ($request->ajax()) {
            $data = Schedule::with('trip', 'busTransport', 'busConductor')->get();
            return Datatables::of($data)
            ->addIndexColumn()
            ->addColumn('trip_code', function($row){
                return $row->trip->trip_code;
             })
             ->addColumn('bus_code', function($row){
                return $row->busTransport->bus_code;
             })
             ->addColumn('bus_conductor_name', function($row){
                return $row->busConductor->bus_conductor_name;
             })
            ->addColumn('action', function($row){
                $btn = '<a href="javascript:void(0)" data-toggle="tooltip"  data-id="'.$row->id.'" data-original-title="Edit" class="btn btn-sm btn-outline-light editRecord">Edit</a>';

                $btn = $btn.' <a href="javascript:void(0)" data-toggle="tooltip"  data-id="'.$row->id.'" data-original-title="Delete" class="btn btn-sm btn-outline-light deleteRecord"><i class="far fa-trash-alt btn-outline-danger"></i></a>';

                return $btn;
            })
            ->rawColumns(['trip_code', 'bus_code', 'bus_conductor_name', 'action'])
            ->make(true);
        }

        return view('admin.schedule', compact('trips', 'busTransports', 'busDrivers', 'busConductors'));
    }

请在您的时间表模型中尝试

public function busTransport(){
  return $this->belongsTo(BusTransport::class, 'bus_id', 'id');
}