You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Wrong table name selected in request when Model Class name is different from table name
###Expected
Use the model $table property in relations if it exist, instead of the Class name for table name.
Steps To Reproduce:
`class ClientOrder extends Model {
protected $table='orders';
public function products() {
return $this->belongsToMany(Product::class);
}
}`
and test in a command :
/** * Execute the console command. * * @return int */ public function handle() { $clientorder=ClientOrder::first(); dd($clientorder->products); return 0; }
Error :
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'orders.client_order_id' in 'field list' (SQL: select products.*, orders.client_order_id as pivot_client_order_id, orders.product_id as pivot_product_id from products inner join orders on products.id = orders.product_id where orders.client_order_id = 4 and products.deleted_at is null)
potential fix : public function products() { return $this->belongsToMany(Product::class,$this->table); }
But it should use the model $table prop if it exist, instead of the Class name
This discussion was converted from issue #37268 on May 05, 2021 21:02.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Description:
Wrong table name selected in request when Model Class name is different from table name
###Expected
Use the model $table property in relations if it exist, instead of the Class name for table name.
Steps To Reproduce:
`class ClientOrder extends Model {
protected $table='orders';
}`
and test in a command :
/** * Execute the console command. * * @return int */ public function handle() { $clientorder=ClientOrder::first(); dd($clientorder->products); return 0; }
Error :
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'orders.client_order_id' in 'field list' (SQL: select
products
.*,orders
.client_order_id
aspivot_client_order_id
,orders
.product_id
aspivot_product_id
fromproducts
inner joinorders
onproducts
.id
=orders
.product_id
whereorders
.client_order_id
= 4 andproducts
.deleted_at
is null)potential fix :
public function products() { return $this->belongsToMany(Product::class,$this->table); }
But it should use the model $table prop if it exist, instead of the Class name
Beta Was this translation helpful? Give feedback.
All reactions