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

phayes0289's avatar

How To Query Another Table if Key Value exists in Current Model

I have a list of fire department apparatus that shows their current status. The page refreshes periodically. The query that displays the apparatus status has a field in it for an IncidentID. If an apparatus gets assigned to an incident the IncidentID that you see there is the ID of the incident found in a separate incidents table. I need to figure out how to display a few of the fields from the incident table in the existing list of incidents when that value is not null. If I set it up as a one to one, will I have any problems when that field is NULL?

<table class="table table-bordered m-0"  wire:poll.10s>
    <thead>
    <tr>
        <th>Unit</th>
        <th>Status</th>
        <th>Current Location</th>
    </tr>
    </thead>
    <tbody>
    @foreach($vehicles as $vehicle)
        <tr>
            <th scope="row">{{$vehicle->Name}}</th>
				<td>{{$vehicle->Master_status}}
                        @if ($vehicle->Master_Incident_ID)
                          {{$vehicle->Master_Incident_ID ?? ''}}
                    @else
                            @endif
                        
            </td>
            <td>{{$vehicle->Current_Location}}
            </td>
        </tr>
    @endforeach
    </tbody>
</table>

Here is what the page looks like. https://www.screencast.com/t/UuqGYCZTwyaA

You can see that M1 and M2 are currently assigned to an incident and currently display the IncidentID under the status. I want to query the incident table and get the IncidentType, and IncidentAddress and display them there instead of the IncidentID, but I am confused on how to do that in Laravel.

0 likes
9 replies
asims's avatar

As long as you do the relevant null checking you should be fine.

So I guess if you have a relation defined such as masterIncident on your vehicle modelyou should be able to do:

{{ $vehicle->masterIncident->incident_type }}
{{ $vehicle->masterIncident->incodent_address }}

You’re already doing a check to see if the vehicle has a master incident ID so you should be able to safely put those in there, obviously if those properties could be null you’ll want to add some default value if necessary. Hope that helps, unless I have misunderstood what you’re asking.

phayes0289's avatar

OK... I thought I caught the problem with a missing foreign key in the vehicles table, but I still cannot get it to work.

Here is my relationship in the CadVehicle model:

    public function cadIncident()
    {
        return $this->hasOne(CadIncident::class);
    }

Here is what I am trying to output on the blade page that has all the units displayed

{{ $vehicle->cadIncident->Problem }}

I keep getting the following error:

Attempt to read property "Problem" on null

I do not see my error. Ugh

Ben Taylor's avatar

@phayes0289 I'm a little confused by your naming, but I'm assuming Master_Incident_ID is the id for the CadIncident? Anyway, if so, you probably need to spell out your foreign key in your relationship as you are breaking convention. Try

public function cadIncident()
    {
        return $this->hasOne(CadIncident::class, 'Master_Incident_ID');
    }

You should then be able to use (check the spelling though) in your blade

{{ $vehicle->cadIncident?->Incident_Type }}
{{ $vehicle->cadIncident?->Incodent_Address }}
phayes0289's avatar

Also tried this on the Model:

 return $this->belongsTo(CadIncident::class, 'ID');
Snapey's avatar

you should have a relationship where a vehicle has many incidents and an incident has many vehicles, and a pivot table to manage the many to many relationship.

Then use the has one of many relationship to get just the latest incident for a vehicle. It will always return one if the vehicle has previous incidents, so in the view, before showing the incident, check if the incident is 'live' before showing it as allocated

https://laravel.com/docs/9.x/eloquent-relationships#has-one-of-many

not sure how you are doing it now, but it may be that you cannot currently easily show a list of incidents that a vehicle has attended?

phayes0289's avatar

@snapey It is kind of hard to explain, but here I go. The table of vehicles basically shows their current GPS location and other meta data. When a vehicle gets assigned to a call, the incident_id field gets updated temporarily with the incident number to which it is assigned to. When the unit clears, that field gets blanked out again. So it is NOT a permanent relationship.

@Ben Taylor .. With the foreign key added via migration, it only returns the units that are actively assigned to an incident. I need all units to display, showing their current location. And only if they get assigned to an incident, I want the incident details to be displayed.

Any suggestions will be greatly appreciated.

Ben Taylor's avatar

Can you please show your vehicles model and incidents model. Also the migrations for the tables that correspond to those models?

Snapey's avatar
Snapey
Best Answer
Level 122

sounds like you need a traditional left join vehicle to incidents table

Please or to participate in this conversation.