i have a model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class MultiProductVariantPivot extends Model
{
//use HasFactory;
protected $table = "multi_product_variant_pivot";
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'multi_product_id',
'variant_id',
'decision_tree',
'hashed_decision_tree'
];
}
I have a query:
$variant_decision_trees = MultiProductVariantPivot::where('multi_product_id', $multi_product_id)->get();
I have an error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'products.multi_product_variant_pivot' in 'where clause' (SQL: select * from `products` where `products`.`multi_product_variant_pivot` = 1 and `products`.`multi_product_variant_pivot` is not null)
Question: Could someone explain to me why Laravel is pointing to the 'products' table (a real table i have) and not the explicitly defined one? How do i stop Laravel overriding my decisions with impunity? Is there a terminal update command that i should have run to refresh something?
EDIT:
I have found another interesting thing, if i change the column name in the where()
to "multi_product_id_test" instead of "multi_product_id" it will reference the correct table..
the new error given:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'multi_product_id_test' in 'where clause' (SQL: select * from `multi_product_variant_pivot` where `multi_product_id_test` = 1)
Thus, the column selection in the where()
is affecting the table selection.. anyone care to explain how to avoid this? also it seems to have added an extra "is not null" clause in the first query, there is defiantly something weird going on.
EDIT 2:
If I change my table name to anything wrong like mproduct_variant
it uses the proper query, if I change it to match an existing table it does the wrong query.. Laravel is trying its hardest to make me not be productive, I'm quite impressed.
EDIT 3: if i change the table name in my model to:
protected $table = "multi_product_variant";
the error i get is:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'products.multi_product_variant_pivot' in 'where clause' (SQL: select * from `products` where `products`.`multi_product_variant_pivot` = 1 and `products`.`multi_product_variant_pivot` is not null)
as can be seen its using products
.multi_product_variant_pivot
instead of multi_product_variant
. could someone explain this behavior? it seems to be caching my old table name? very strange.
CodePudding user response:
That's because you are naming your model with "Pivot" suffix, which is interfering with Laravel's many-to-many relationship system and not the best practice. What you can do is "force" Laravel by telling it which table to use:
$variant_decision_trees = MultiProductVariantPivot
::where(`multi_product_variant_pivot.multi_product_id`, $multi_product_id)->get();
That's the possibility that I can think of, it may not be the root tho. And for the love of god. Follow the convention if you can.
CodePudding user response:
okayy so here it is, i had a model called MultiProduct
with a function to relate the variants of the product like so:
public function variants(){
return $this->hasMany( 'App\Models\Product', 'multi_product_variant_pivot');
}
so what was happening was my MultiProductVariant
model was being translated into activating the variants()
function from the MultiProduct
model. I changed it to be:
public function products(){
return $this->hasMany( 'App\Models\Product', 'multi_product_variant');
}
and now it works because its not being linked! Dont ask me why, I'm just a consumer of this framework. Crazy stuff.