I am a beginner of laravel. I want to join some of the tables and getting it displayed in datatables, I am using yajra datatables. I have four tables which are Schedule, Trip, BusTransport and BusConductor. The issues that I facing right now is I wan to display Schedule in a blade view, I want to get trip_id from Trip Table, bus_id from BusTransport Table and bus_conductor_id from BusConductor Table. I able to display the data for Trip and BusConductor but I keep getting an error for one of the field from BusTransport. It shows me that DataTables warning: table id=example - Exception Message: Trying to get property 'bus_code' of non object. Can anyone help me to spot my problem? Thanks and appreciate.
Trip 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();
});
}
BusTransport 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();
})
Schedule 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');
});
}
Trip Model
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);
}
}
BusTransport Model
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 Model
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);
}
}
Schedule Model
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 request for Schedule datatables
<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
},
]
});
Schedule Controller
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'));
}
CodePudding user response:
Please try in your Schedule Model
public function busTransport(){
return $this->belongsTo(BusTransport::class, 'bus_id', 'id');
}