dimanche 26 avril 2020

Ambiguous column name laravel 5.6+

i have a problem with error called "Column 'id' in field list is ambiguous".

I've tried to fix it by using aliases on columns but i had no luck in fixing it. Query looks like this:

SELECT `id`               AS `pf_id`, 
       `id_feature`       AS `pf_if`, 
       `id_feature_value` AS `pf_ifv`, 
       `product_features`.`id_product` 
FROM   `features` 
       INNER JOIN `product_features` 
               ON `product_features`.`id_feature` = 
                  `features`.`pf_id` 
WHERE  `product_features`.`id_product` IN ( 
       33003, 33004, 33011, 33012, 
       33013, 33015, 33016, 33017, 
       33018, 33019, 33020, 33021, 
       33022, 33023, 33024, 33025, 
       33026, 33029, 33030, 33032 ) 
       AND `id_feature` = 5 

Id is used only in select and inner join. It's calling product_features table and features table. Only this pice of code is not working - any other relation on product is working fine. Below are additional info about table structure plus relation:

$products = Product::select('id', 'id as DT_RowId', 'id_manufacturer', 'sku', 'price_retail', 'active')
            ->with([
                'manufacturer' => function ($m) {
                    $m->with(['defaultTranslation' => function ($query) {
                        $query->select('id', 'id_manufacturer', 'name');
                    }]);
                },
                'defaultTranslation' => function ($dt) {
                    $dt->select('id', 'id_product', 'name');
                },
                'features' => function ($qf) {
                    $qf->select('id as pf_id', 'id_feature as pf_if', 'id_feature_value as pf_ifv');
                    $qf->where('id_feature', 5);
                },
            ]);

$result = $products->orderBy('id', 'DESC')->paginate($itemNumber, ['*'], 'page', $page);

product_features structure:

enter image description here

features:

enter image description here

Features relation (function from model)

public function features()
    {
        return $this->hasManyThrough('App\SupremeShop\Models\Feature',
            'App\SupremeShop\Models\ProductFeature',
            'id_product',
            'id',
            'id',
            'id_feature');
    }

Thanks for help!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire