I would add more relationships there.
Task belongsToMany vehicles. Vehicle belongsToMany tasks.
$tasks = Task::with('vehicles.configuration')->paginate();
forech ($tasks as $task) {
for($task->vehicles as $vehicle) {
// Do something with the data
$task->start ...
$task->end ...
$vehicle->id ...
$vehicle->configuration->configuration ...
}
}
In my database have 10 different vehicles used in randomly for each tasks. In task A could have vehicles 1 and 2; in task B could have vehicles 3 and 6; in task C could have vehicles 1, 5, 6, 8 and 10.
So using VehicleTasks table I can know which vehicles are been used for a single task and via Vehicle and Configuration tables know vehicle identification and configuration.
In VehicleTasks table I record also km_start and km_end to know how many kilometers run for that task with that vehicle.
I hope I haven't confused you. :-)
$vehicleTasks = VehicleTasks::with('task','vehicle.configuration')->get();
$vehicleTasks->transform(function($vehicleTask) {
return [
'start' => $vehicleTask->task->start,
'end' => $vehicleTask->task->end,
'vehicle_identification' => $vehicleTask->vehicle->identification,
'vehicle_configuration' => $vehicleTask->configuration->configuration,
];
});
akibo liked this reply
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community