public function vehicle_type_slots()
{
return $this->belongsToMany(VehicleTypeSlot::class, 'vehicle_type_slot')->withPivot(['fare']);
}
Have you tried to name the relationship table?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
using below code:
$vehicle_type = $request->vehicle_type;
$vtype =array('vehicle_type'=> $vehicle_type);
$vehicletype = VehicleType::create($vtype);
$mileage_slot = $request->input('mileage_slot', []);
$fare = $request->input('price', []);
for ($i=0; $i < count($mileage_slot); $i++) {
if ($mileage_slot[$i] != '') {
$vehicletype->vehicle_type_slots()->attach($mileage_slot[$i], ['fare' => $fare[$i]]);
}
model VehicleType
public function vehicle_type_slots()
{
return $this->belongsToMany(VehicleTypeSlot::class)->withPivot(['fare']);
}
Its creating following query for detail table with wrong table name
insert into
vehicle_type_vehicle_type_slot(fare,vehicle_type_id,vehicle_type_slot_id) values (?, ?, ?)"`
it should be
insert into vehicle_type_slot (fare, vehicle_type_id, mileage_slot) values (?, ?, ?)"
public function vehicle_type_slots()
{
return $this->belongsToMany(VehicleTypeSlot::class, 'vehicle_type_slot')->withPivot(['fare']);
}
Have you tried to name the relationship table?
Please or to participate in this conversation.