【发布时间】:2021-11-30 20:56:25
【问题描述】:
我是 laravel 的初学者。我想加入一些表并将其显示在数据表中,我正在使用 yajra 数据表。我有四个表,分别是 Schedule、Trip、BusTransport 和 BusConductor。我现在面临的问题是我想在刀片视图中显示日程安排,我想从 Trip Table 获取 trip_id,bus_id 从BusTransport Table 和 bus_conductor_id 来自 BusConductor Table。我能够显示 Trip 和 BusConductor 的数据,但我不断收到来自 BusTransport 的字段之一的错误。它向我展示了 DataTables 警告:table id=example - 异常消息:尝试获取非对象的属性“bus_code”。谁能帮我发现我的问题?感谢并感谢。
行程表
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();
});
}
BusConductor 表
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);
}
}
计划数据表的 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" 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'));
}
【问题讨论】:
-
试试这个 ->addColumn('bus_code', function($row){ return $row->bus_transport->bus_code; }) 并对 busConductor 做同样的事情
-
嗨@HuzaifaQidwai 感谢您的评论。这似乎不适合我。我正在考虑我的 Schedule Model 和 BusTransport Model 的问题。因为我只是对我的 Trip 和 BusConductor 使用相同的逻辑,所以它们没有任何问题。
标签: ajax laravel datatables yajra-datatable