Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kidi's avatar
Level 22

Which relationship is this?

I have a customer, batteries and replaced_batteries table.

How can I set the proper relationship, if I replace a battery?

Let me explain:

batteries table:
id: 1
customer_id: 1

This battery goes wrong, replace with a new, then the tables should looks like:

batteries table:
id: 1
customer_id: 1

id: 2
customer_id: 1

replaced_batteries table:

id: 1
old_battery_id: 1
new_battery_id: 2

And if the second battery is goes wrong too, then:

batteries table:
id: 1
customer_id: 1

id: 2
customer_id: 1

id: 3
customer_id: 1

replaced_batteries table:

id: 1
old_battery_id: 1
new_battery_id: 2

id: 1
old_battery_id: 2
new_battery_id: 3

Which is the best method for this? I want to chain the replaced batteries to the root battery.

0 likes
6 replies
lostdreamer_nl's avatar

Why not keep the replaced ones in the batteries table? A battery is a battery after all.

Just put a column (boolean) in there replaced and add a nullable replaced_with_id in there, linking back to batteries.id

There is no need to create a new table for that.

In that case, replacing the battery would be:

  1. Add a new battery for this customer to the table
  2. flip the replaced boolean on the current battery row to 1
  3. update the old battery with replaced_with_id = {old_id}

otherwise, you'd be adding a battery to the new table, then replicating the old battery from 1 table to another (also updating it's new_battery_id), and deleting the old row.

1 like
kidi's avatar
Level 22

Thanks for your reply @lostdreamer_nl !

Okay, so let sketchup this.

We have a batteries table, like this:

id: 1 replaced: 0 replaced_with_id: null created_at: 2017.07.05

id: 2 replaced: 1 replaced_with_id: 3 created_at: 2018.07.10.

id: 3 replaced: 1 replaced_with_id: 4 created_at: 2018.07.11.

id: 4 replaced: 0 replaced_with_id: 0 created_at: 2018.07.12.

How can I fetch the batteries, with the replaced batteries in JSON?

I want something like this:

ID: 1 - Bought battery at 2017.07.05. ID: 2 - Bought battery at 2018.07.10. - Replaced at 2018.07.11. - Replaced at 2018.07.12.

lostdreamer_nl's avatar
Level 53

You could do something like this in a view:

// controller:
$batteries = $customer->batteries->keyBy('id');

// view
<table>

@foreach($batteries as $battery) {
    <tr>
        <td>{{ $battery->id }}<td>
        <td>{{ $battery->created_at }}<td>
        <td>
            @if($battery->replaced_with_id)
                Replaced at: {{ $batteries[$battery->replaced_with_id]->created_at }}
            @endif  
        <td>
    <tr>
}
</table>

By using the id as the key of your collection, you can use $batteries[$someId]->created_at to get the replacement battery's creation date.

1 like
kidi's avatar
Level 22

Thank you my friend! It's works! :)

lostdreamer_nl's avatar

@kidi , 1 more thing, you can just get rid of that 'replacement' boolean, and check for !is_null(replaced_with_id)

That way the data can not be corrupt (what would happen if, for some bug anyone has replacement = 1, replacement_id = NULL ?) and the system will be (a bit) more stable.

No problem btw, glad to help ;)

1 like

Please or to participate in this conversation.