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

vincent15000's avatar

Problem with a one-to-many relationship ...

Hello,

I have a very strange problem with an app on which I work and which has been started by another dev.

Here are the tables.

brands : id, name
bicycles : id, name, brand			// the brand is a string in the database
quotation : id, bicycle_id

And the quotation model with the relationship.

public function bicycle()
{
	return $this->belongsTo('App\Models\Bicycle');
}

But there is no relationship with the Brand model, neither with the Bicycle nor with the Quotation models.

All worked fine until an update of the one brand (update imported from an Excel file). When I list the quotations, I use $quotation->bicycle to access the bicycle from the quotation. All is ok except for the bicycles with the brand which has been updated.

In the database all is ok, the foreign key is ok in the quotation line, but when I try to access the bicycle from the quotation in Laravel, I have this error.

Trying to get property 'brand' of non-object

I have tested : $quotation->bicycle is null.

That's very strange because all datas are ok in the database. I have done php artisan to clear and cache the config, the routes, the views, ... but no result to solve the problem.

So my question is : do you have already encountered such a problem and what could be the cause of the problem ?

Thanks for your help.

Vincent

0 likes
27 replies
MichalOravec's avatar

It's not a problem with relationship, but with your data.

That's very strange because all datas are ok in the database.

Obviously they are not ok.

$quotations = Quotation::doesntHave('bicycle')->get();

// or

$quotations = Quotation::whereNull('bicycle_id')->get();

With that you will see what is wrong and then fix your data in the database.

vincent15000's avatar

Thank you.

$quotations = Quotation::doesntHave('bicycle')->get();

=> I retrieve the quotation with the problem and the bicycle_id is ok in the quotation.

$quotations = Quotation::whereNull('bicycle_id')->get();

=> I retrieve an empty array.

MichalOravec's avatar
$quotations = Quotation::doesntHave('bicycle')->get();

So with that you retrieve a quotation or quotations with bicycle_id which doesn't exist in bicycles table in column id of course.

vincent15000's avatar

That's the problem : I retrieve a quotation with a bicycle_id which exists in the bicycles table in column id. That's why it's very very strange.

select * from bicycles where id = '53ca6f30-5a39-11ea-9fd6-d930c5b6158c';

One row result.

jlrdw's avatar

Sounds like during an import, child data isn't being imported or saved. Is there a way to contact the other Dev to clarify the import procedure that works.

vincent15000's avatar

That's also the problem, the dev doesn't respond any more.

vincent15000's avatar

Child data ? What do you mean ? I understand child data, but I don't understand in my context because the foreign key is ok in the database and the brand field is not empty in the bicycle line.

Snapey's avatar

if you get this Trying to get property 'brand' of non-object inside a loop it means that one of your quotations does not have a related model

you should protect for this possibility by using the null coalesce operator, eg

$quotation->bicycle->brand ?? ' missing'

post the code that throws the error for specific advice

vincent15000's avatar

Here is the code.

@foreach($quotations as $quotation)
    <tr  class="tooltip-demo" >
		<td>
            <h4 class="my-0">{{ $quotation->bicycle->brand }}</h4>
            <p class="mb-0">{{ $quotation->bicycle->model }} ( {{ $quotation->bicycle->year }} ) @if($quotation->bicycle->electric) <span class="text-info"><i class="fa fa-bolt" data-toggle="tooltip" data-placement="top" title="Electric bicycle"></i> </span>@endif
			</p>
		</td>
	</tr>
@endforeach

It's ok to protect in my view ... but what's strange, as I said in the previous posts, is that the quotation which is the problem HAS a related model => I have verified in the database and the foreign key exists and is ok => BUT Laravel seems not to recognize this related model ?

Snapey's avatar

change your code as below and see what happens

@foreach($quotations as $quotation)
    <tr  class="tooltip-demo" >
		<td>
            <h4 class="my-0">{{ $quotation->bicycle->brand ?? 'error' }}</h4>
            <p class="mb-0">{{ $quotation->bicycle->model  ?? 'error' }} ( {{ $quotation->bicycle->year ?? 'error'  }} ) @if($quotation->bicycle->electric ?? 'error' ) <span class="text-info"><i class="fa fa-bolt" data-toggle="tooltip" data-placement="top" title="Electric bicycle"></i> </span>@endif
			</p>
		</td>
	</tr>
@endforeach
vincent15000's avatar

It works with that code. It displays "error".

Just below I have posted the datas from the database, do you see something not normal ?

vincent15000's avatar

Here are the datas for the quotation with the problem and its bicycle, perhaps it will help.

The quotation with the problem.

    "id" => "024c14a0-7148-11eb-9ae7-d7a151fa13e0"
    "user_id" => "dbddd9a0-f726-11e9-acda-ef7d4f99c8d0"
    "bicycle_id" => "53ca6f30-5a39-11ea-9fd6-d930c5b6158c"

And the bicycle.

	"id" => "53ca6f30-5a39-11ea-9fd6-d930c5b6158c"
	"brand" => "Merida"
	"model" => "Scultura 4000"
	"year" => "2018"
MichalOravec's avatar

Why do you have in the select velos?

select * from velos where id = '53ca6f30-5a39-11ea-9fd6-d930c5b6158c';
vincent15000's avatar

Oh sorry ... for the forum here I have translated with bicycle ... but the code is in french.

MichalOravec's avatar

Always write a code in English and database tables and columns as well.

vincent15000's avatar

Yes that's obvious. But this is an app another dev has started and now it's me who works on it. This app seems to be full of bad code.

vincent15000's avatar

Do the datas let you think about something to approach the soluce ?

Snapey's avatar

since you use non-incrementing ids do you have public $incrementing=false; in your models

vincent15000's avatar

I have deleted the bicycle and the quotation. Then I have created exactly the same bicycle and the same quotation.

And I have the same problem.

Snapey's avatar

what column types on the database?

vincent15000's avatar

The column types are char(36) for the ids and for the foreign keys. And for the brand it's varchar(), but I don't think that the brand is the problem.

When the brand has been updated, the script which imports the datas from the Excel file has updated the bicycle (but not the quotation). So the problem comes from the bicycle.

vincent15000's avatar

I have it ... in the bicycles table, there's a deleted_at field which has a date. So Laravel consider the bicycle doesn't exist anymore.

While importing the updated datas for the bicycle, the script surely update the bicycle setting deteled_at to the date of the update.

Now I have to find why.

Well thank you for your help and the different suggestions.

vincent15000's avatar

I don't know who has the best answer between @snapey and @michaloravec ... you both helped me very well and your answers have oriented me to the soluce ;).

Thanks a lot ;).

Please or to participate in this conversation.