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

rszabo@crowleyauto.net's avatar

Livewire Model with Multiple relationships

I have a model with multiple hasOne relationships. Only 1 of the relationships seems to work with livewire. I know the relationships work in laravel, I test them with tinker and I'm able to get the related table data. The relationships that don't work are being interrupted as fields when trying to do an update on the model, and I can't display the data in the wire model.

This works

<x-text wire:model="rig.canopy.relined" id="relined" name="relined"/>

This does not

<x-text wire:model="rig.reserve.model" id="model" name="model"/>

Model

class Rig extends Model
{
    use HasFactory;
    protected $dates = ['reserve_due', 'reserve_packed'];
    protected $appends = ['reservestatus'];
    protected $guarded = [];


    public function reserve()
    {
        return $this->hasOne(Reserve::class);
    }

    public function canopy()
    {
        return $this->hasOne(Canopy::class);
    }

    public function aad()
    {
        return $this->hasOne(Aad::class);
    }
}

Rules

  protected $rules = [
        'rig.rig_model' => 'required|string|min:3',
        'rig.rig_ser' => 'string|min:3|nullable',
        'rig.rig_type' => 'nullable',
        'rig.rig_dom' => 'sometimes',
        'rig.reserve_packed' => 'date',
        'rig.reserve_due' => 'date',
        'rig.reserve.manufacturer' => 'sometimes',
        'rig.canopy_id' => 'sometimes',
        'rig.reserve_ser' => 'sometimes',
        'rig.canopy.manufacturer' => 'sometimes',
        'rig.canopy.model' => 'sometimes',
        'rig.canopy.size' => 'sometimes',
        'rig.canopy.serial_number' => 'sometimes',
        'rig.canopy.dom' => 'sometimes',
        'rig.canopy.colors' => 'sometimes',
        'rig.canopy.relined' => 'sometimes',
        'rig.canopy.status' => 'sometimes',
        'rig.reserve.model' => 'sometimes',
        'rig.aad.model' => 'sometimes',
    ];

In the debug data I get this. You can see the canopy relation works, but the other 2 show up as empty arrays and are considered fields. And there are data in those tables for reserve and aad

{
    "rig_model": "Sigma 01",
    "rig_ser": "58005",
    "rig_type": "1",
    "rig_dom": "01/01/2014",
    "reserve_packed": "05/25/2020",
    "reserve_due": "11/21/2020",
    "canopy_id": 1,
    "reserve_ser": "004301",
    "reserve": [],
    "canopy": {
        "manufacturer": "Sigma",
        "model": "Sigma II 340",
        "size": "340",
        "serial_number": "SG-340 001665",
        "dom": "06-01-2011",
        "colors": "Purple/Green/Yellow",
        "relined": "06/01/2015",
        "status": "1"
    },
    "aad": [],
    "class": "App\Models\Rig",
    "id": 1,
    "relations": [
        "canopy"
    ],
    "connection": "mysql"
}

Tinker output

=> App\Models\Rig {#4103
     id: 1,
     rig_type: "1",
     rig_model: "Sigma 01",
     rig_dom: "2014-01-01",
     rig_ser: "58005",
     canopy_model: "Sigma II 340",
     canopy_dom: "2011-06-01",
     canopy_lineset: "2015-06-01",
     canopy_color: "Purple/Green/Yellow",
     canopy_ser: "SG-340 001665",
     reserve_model: "PD 360",
     reserve_dom: "2001-01-01",
     reserve_ser: "004301",
     reserve_packed: "2020-05-25",
     reserve_due: "2020-11-21",
     aad_model: "Cypres 2",
     aad_ser: "L7971",
     aad_dom: "2020-08-01",
     aad_4yr_due: "2024-08-01",
     aad_8yr_due: "2028-08-01",
     aad_retire: "2032-08-01",
     aad_lst_battery: "2024-08-01",
     aad_nxt_battery: "2028-08-01",
     rig_status: 0,
     who_packed: "MT",
     canopy_id: 1,
     reserve_id: null,
     aad_id: null,
     +reservestatus: "red",
   }

>>> $a->reserve
=> App\Models\Reserve {#4715
     id: 1,
     model: "PD 360",
     size: "360",
     manufacturer: "PD",
     serialnumber: "004301",
     dom: "2001-1-1",
     colors: null,
     repacks: null,
     status: null,
     user_id: null,
     rig_id: 1,
     created_at: null,
     updated_at: null,
   }
0 likes
4 replies
Snapey's avatar

show the code that loads the model and its relations please

rszabo@crowleyauto.net's avatar

Livewire

class RigEdit extends Component
{
    public Rig $riig;
   public $canopies;

    protected $listeners = ['update' => 'updateRepack'];
  
  protected $rules = [
        'rig.rig_model' => 'required|string|min:3',
        'rig.rig_ser' => 'string|min:3|nullable',
        'rig.rig_type' => 'nullable',
        'rig.rig_dom' => 'sometimes',
        'rig.reserve_packed' => 'date',
        'rig.reserve_due' => 'date',
        'rig.reserve.manufacturer' => 'sometimes',
//        'rig.reservestatus' => 'sometimes',
//        'rig.repack-due' => 'sometimes',
        'rig.canopy_id' => 'sometimes',
        'rig.reserve_ser' => 'sometimes',
        'rig.canopy.manufacturer' => 'sometimes',
        'rig.canopy.model' => 'sometimes',
        'rig.canopy.size' => 'sometimes',
        'rig.canopy.serial_number' => 'sometimes',
        'rig.canopy.dom' => 'sometimes',
        'rig.canopy.colors' => 'sometimes',
        'rig.canopy.relined' => 'sometimes',
        'rig.canopy.status' => 'sometimes',
        'rig.reserve.model' => 'sometimes',
        'rig.aad.model' => 'sometimes',
    ];

    public function render()
    {
        $this->canopies = Canopy::where('status', 1)->get();

        return view('livewire.rig-edit');
    }
rszabo@crowleyauto.net's avatar

Route

    Route::get('/rig/edit/{rig}', \App\Http\Livewire\RigEdit::class)->name('rig-edit');
rszabo@crowleyauto.net's avatar

So if I add @json($rig->reserve) then it works fine. Does this have something to do with eager loading? Are the relations not actually loading?

Please or to participate in this conversation.