mardi 19 juin 2018

How to make eloquent filter result based on belongs to many relation withmultidimensional request array

I'm trying to filter results from a table based on conditions on another table's attributes.

Actually, I know how to do simple queries base on another table but my problem consists of two basic problems: 1. how to group these queries based on the request parameters 2. how to do this with withmultidimensional request array

I have 2 tables: 1. products: has many attributes

public function attributes(){

        return $this->hasMany(Attribute::class,'product_id');
    }

2:attributes: belongs to product

--------------------------------------------
|    code   |   value         | product_id |
-------------------------------------------
|   AA127   | 14 ios          |      1     | 1 -> stands for iphone
|   AA127   |  andriod        |      2     | 2 -> stands for samsung
|           |                 |            |
|   AA113   | 2 singlesimcard |      1     |
|   AA113   | doublesimcard   |      2     |
|           |                 |            |
-------------------------------------------

To specify what I'm really trying to say, this is the request url that sent to controller:

  http://**BASEURL**/search?category[0]=2&attr[AA127][0]=14&attr[AA127][1]=22
&attr[AA113][0]=2&attr[AA113][1]=3

You see the request parameters are arrays grouped by attribute codes. and the parameters array ($request->all()) looks like this:

array:2 [▼
  "category" => array:1 [▼
    0 => "2"
  ]
  "attribute" => array:2 [▼
    "AA127" => array:1 [▼
      0 => "14"
    ]
    "AA113" => array:1 [▼
      0 => "2"

    ]
  ]
]

So I wrote the query this way:

          $products = $products->when($tecInfos, function ($query) use ($attributes) {

                foreach ($attributes as $attributeIds) {

                    $query = $query->where(function ($q) use ($attributeIds) {

                        foreach ($attributeIds as $attrId){

                            $value = TecInfoProduct::where('tec_id',$attrId)->first();

                            $q = $q->orWhere('value', 'like', '%' . $value->value . '%');

                        }
                        return $q;
                    });
                }
                return $query;
            });

So this will group the attributes and when I run:

$products->toSql();

It gives me this query:

select * from `products` where (`category_id` = ?) and 

exists (select * from `tec_info_products` where `products`.`id` = 
`attributes`.`product_id` and 

(`value` like 14 ) and (`value` like 2 ))

So this query exactly what I want. it groups the atrrs by code and put and between them and in the parenthesis it puts or in between.

But it gives me no results. when I run the query one by one it shows the related result. Even if I put attributes from the same group like AA127[1] = 14 & AA127[0] = 22 it brings the result again but with different groups, it has no results.

so what did I wrong? how am I suppose to run the grouped queries?

Thanks In Advance...!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire