我是 Laravel 的初學者。我想加入一些表并將其顯示在資料表中,我正在使用 yajra 資料表。我有四個表,分別是 Schedule、Trip、BusTransport 和 BusConductor。我面對現在的問題是我婉在刀片視圖中顯示計劃,我想trip_id從行程表,bus_id從BusTransport表和bus_conductor_id從BusConductor表。我能夠顯示 Trip 和 BusConductor 的資料,但我一直收到來自 BusTransport 的欄位之一的錯誤。它向我展示了DataTables 警告:table id=example - Exception Message: Trying to get property 'bus_code' of non object. 誰能幫我找出我的問題?謝謝和欣賞。
行程表
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();
});
}
巴士交通表
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();
});
}
母線表
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();
})
日程表
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);
}
}
母線模型
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);
}
}
對 Schedule 資料表的 Ajax 請求
<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" >Edit</a>';
$btn = $btn.' <a href="javascript:void(0)" data-toggle="tooltip" data-id="'.$row->id.'" data-original-title="Delete" ><i ></i></a>';
return $btn;
})
->rawColumns(['trip_code', 'bus_code', 'bus_conductor_name', 'action'])
->make(true);
}
return view('admin.schedule', compact('trips', 'busTransports', 'busDrivers', 'busConductors'));
}
uj5u.com熱心網友回復:
請在您的計劃模型中嘗試
public function busTransport(){
return $this->belongsTo(BusTransport::class, 'bus_id', 'id');
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/314704.html
