Support the ongoing development of Laravel.io →
posted 3 years ago
Last updated 1 year ago.
0

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 ...
    }
}
0

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. :-)

0
$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,
    ];
});
Last updated 3 years ago.

akibo liked this reply

1

Sign in to participate in this thread!

Eventy

Your banner here too?

Benedetto bennyatc Joined 29 Jun 2020

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.