i have a vehicle table. Vehicles are transferable. When a vehicle is transferred to the new owner, another row is added to the table with different details. Old vehicle's vehicles_transfer
column is filled with the id of that new row.
Vehicles can be transferred multiple times. Here's the vehicle table:
+--------------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| vehicles_vin | varchar(255) | NO | UNI | NULL | |
| vehicles_model_id | int(11) | NO | | NULL | |
| vehicles_transfer | int(11) | YES | | NULL | |
+--------------------+------------------+------+-----+---------------------+-----------------
Now i'm stuck at a point. I have a vehicle which is transferred 5 times.
When it was registered for the first time, its id was 5
and it was transferred to a new owner having vehicle table id 7
. So vehicles_transfer
column was filled with 7
. vehicles_transfer
column says that this vehicle is transferred to the filled id.
Then vehicle id 7
was transferred further to id 12
, So, its vehicles_transfer
column was filled with 12.
This process has happened 3 more time.
Now i have the id of the current vehicle and i want to know how many times this vehicle has been transferred previously. i have also made a function getOldVehicleId()
which takes id
of the vehicle and provides the vehicles_transfer
value.
But i'm not getting how to get all old vehicles_transfer
values?
Edit: Basically i want to make this dynamic
$oldVehicle1 = Vehicle::withTrashed()->where('vehicles_transfer', $id)->first();
if($oldVehicle1){
$oldVehicleIds[] = $oldVehicle1->id;
$oldVehicle2 = Vehicle::withTrashed()->where('vehicles_transfer', $oldVehicle1->id)->first();
if($oldVehicle2){
$oldVehicleIds[] = $oldVehicle2->id;
$oldVehicle3 = Vehicle::withTrashed()->where('vehicles_transfer', $oldVehicle2->id)->first();
if($oldVehicle3){
$oldVehicleId[]s = $oldVehicle3->id;
$oldVehicle4 = Vehicle::withTrashed()->where('vehicles_transfer', $oldVehicle3->id)->first();
}else return $oldVehicleIds;
}else return $oldVehicleIds;
}