1 year ago
#386123
treb
Eloquent/query relationship bug?
I have a Customer
model which hasMany CustomerContactInfo
and hasMany CustomerBillingInfo
relationships.
In the controller update
I get a payload and it contains data for all the three models and I seperate them in the controller. When I want to update the models I have:
($customer = Customer::find($request->route('id')))->update($customerData);
$customer->billing_info->first()->update($customerBillingInfo);
$customer->contact_info->first()->update($customerContactInfo);
Before you asking why I have hasMany relationship when I only using the first one is because in the first version of my app will only has one billing info and contact info. In the future it will contains many so I'm halfly preparing for that event.
I wrote a test for this route and the test gave back 200. Hovewer when I try on the frontend it give me an SQL error and laravel inject where 'id' is null
in the update query but only for the contact_info upate query. I know the solution for my problem is that I did not have a primary key before, because right know the customer_billing_infos
and customer_contact_infos
have customer_id
and no id
column. And I can solve my problem if I insert protected $primaryKey = 'customer_id';
line in the CustomerContactInfo model (and also the billing info). I know I should have and I will insert id column for all the contact info and billing info records, but know what I don't understand is the source of the problems:
- How is the frontend throwed and error while test is not? I logged all the data I get in the controller to compare what I get from both. The datas are identical. Same variable names and variable count.
- How is the billing info update did not throw and error while it has the same exact relationship as contact info? It has the same belongsTo and hasMany relationship in the models and it does not have any
id
column either. I tried to change the update order in the controller, even I changed the relationships order in the models.
Is this a bug or laravel cache something that I don't know or what is happening? (using Laravel 9)
php
laravel
eloquent
orm
0 Answers
Your Answer