E ticaret sistemi kategori filtreleme problemi
mgsmus teşekkürler hocam şu şekilde aslında yapı:
renk=5|2 ayrışmasında | ile ayırdığımda soldaki value değeri sağdaki options değeri
pivot tablomda bu şekilde
id
variation_id
product_id
options_id
value_id
ilk click işleminde tek varyasyon seçimi sonrakilerinde de url üzerine ekleme yaparak gidiyorum
yani gelen yapıda renk=5|2&beden=19|3
options_id 2 olan ve value_id 5 olanları seçtikten sonra, beden seçildiğinde tekrar aynı noktada
options_id 3 olan ve value_id 19 olanları seçtirmeye çalışıyorum. Burada ilk gelen verileri bir yerde tutup bunlar içerisinden seç dediğimde de sonucu sıfırlayıp çıkıyor. Kodun son hali bu:
`
$request_arr = request()->except("filtre");
foreach ($request_arr as $get_key => $item_get) {
$resp = explode("|", $item_get);
$resp_opt = strip_tags($resp[1]); // $resp[1] pivottaki options_id
$resp_val = strip_tags($resp[0]); // $resp[0] pivottaki value_id
$whereInOptions[] = $resp_opt;
$whereInValues[] = $resp_val;
// $result->whereIn( 'product_options_pivot.options_id', [ implode(",", $whereInOptions) ] );
// $result->whereIn( 'product_options_pivot.value_id', [ implode(",", $whereInValues) ] );
$result->where( ['product_options_pivot.options_id' => $resp_opt, 'product_options_pivot.value_id' => $resp_val ] );
unset($resp_opt); unset($resp_val);
}
`
- Düzenlendi
$filters = [];
foreach ($request->query() as $key => $value) {
if(strpos($value, '|') !== false) {
$filter = explode('|', $value);
$filters[] = [
'option_id' => $filter[0],
'value_id' => $filter[1],
];
}
}
$result->when($filters, function($query, $filters) {
$query->where(function($query) use ($filters) {
foreach($filters as $filter) {
$query->orWhere(function($query) use ($filter) {
$query->where('product_options_pivot.options_id', $filter['option_id'])
->where('product_options_pivot.value_id', $filter['value_id']);
});
}
});
});
Bu şöyle bir SQL oluşturur:
WHERE (
(product_options_pivot.options_id = 5 AND product_options_pivot.value_id = 2)
OR (product_options_pivot.options_id = 19 AND product_options_pivot.value_id = 3)
);
- Düzenlendi
mgsmus teşekkürler hocam olmadı buda
Sql çıktı aldığımda ve bunu manuel çalıştırdığımda 2 ürün birden geliyor oysa sadece biri gelmesi gerekiyor çünkü 19 ID'si 40 numara bir bedene ait tek bir ürün kalıyor.
SQL çıktısı:
SELECT
selectproduct.*,
product_variation.id AS pro_var_id,
product_variation.title AS pro_var_title,
product_variation.slug AS pro_var_slug,
product_variation.sku AS pro_var_sku,
product_variation.price AS pro_var_price,
product_variation.stock AS pro_var_stock
FROM
product_category_pivot
INNER JOIN
product ON product.id = product_category_pivot.product_id
INNER JOIN
product_variation ON product_variation.product_id = product_category_pivot.product_id
INNER JOIN
product_options_pivot ON product_options_pivot.product_id = product_category_pivot.product_id
WHERE
(
product_category_pivot.category_id = 27
AND product_variation.show_primary = 1
AND product.deleted_at IS NULL
AND product.publish = 0
)
AND (
(product_options_pivot.options_id = 2 AND product_options_pivot.value_id = 5)
OR
(product_options_pivot.options_id = 19 AND product_options_pivot.value_id = 3)
)
GROUP BY
product_variation.id
ORDER BY
product.id DESC;
buda sorgu sonucu
https://prnt.sc/-h8H0bw37UYN
mgsmus değiştirdim hocam sql çıktı bu şekilde;
select
product.*,
product_variation.
idas
pro_var_id,
product_variation.
titleas
pro_var_title,
product_variation.
slugas
pro_var_slug,
product_variation.
skuas
pro_var_sku,
product_variation.
priceas
pro_var_price,
product_variation.
stockas
pro_var_stockfrom
product_category_pivotinner join
producton
product.
id=
product_category_pivot.
product_idinner join
product_variationon
product_variation.
product_id=
product_category_pivot.
product_idinner join
product_options_pivoton
product_options_pivot.
product_id=
product_category_pivot.
product_idwhere (
product_category_pivot.
category_id= 27 and
product_variation.
show_primary= 1 and
product.
deleted_atis null and
product.
publish` = 0) and
(
(product_options_pivot
.options_id
= 2 and product_options_pivot
.value_id
= 5)
and
(product_options_pivot
.options_id
= 3 and product_options_pivot
.value_id
= 19)
) group by product_variation
.id
order by product
.id
desc`
kaç varyasyon olursa olsun 0 ürün getiriyor
- Düzenlendi
mgsmus hocam;
kategori sayfamda ürünleri çekmek için $category->getProducts şeklinde modelden çağırıyorum.
getProducts metodu içerisi şu şekilde;
`public function getProducts($id = null)
{
if($id==null){ $pass_id = $this->id; }else{ $pass_id = $id; } // kategoride ki benzer ürünler için oluşturuldu
$result = DB::table('product_category_pivot')
->where(['product_category_pivot.category_id' => $pass_id, 'product_variation.show_primary' => 1, 'product.deleted_at' => null, 'product.publish' => 0])
->join('product','product.id','=','product_category_pivot.product_id')
->join('product_variation','product_variation.product_id','=','product_category_pivot.product_id')
->join('product_options_pivot','product_options_pivot.product_id','=','product_category_pivot.product_id') ## varyasyon filtre için sonradan ekledim
->select(
'product.*',
'product_variation.id as pro_var_id','product_variation.title as pro_var_title','product_variation.slug as pro_var_slug',
'product_variation.sku as pro_var_sku','product_variation.price as pro_var_price','product_variation.stock as pro_var_stock'
)
->groupBy('product_variation.id')
->orderBy('product.id','desc');
$request_arr = request()->except("filtre");
if( isset($request_arr["marka"]) ) {
$result->join('brand_pivot', function ($join) {
$join->on('brand_pivot.product_id', '=', 'product.id')->where('brand_pivot.brand_id', '=', strip_tags($request_arr["marka"]));
});
unset($request_arr["marka"]);
}
unset($request_arr["fiyat"]);
$filters = [];
foreach ($request_arr as $key => $value) {
if(strpos($value, '|') !== false) {
$filter = explode('|', $value);
$filters[] = [
'option_id' => $filter[0],
'value_id' => $filter[1],
];
}
}
$result->when($filters, function($query, $filters) {
$query->where(function($query) use ($filters) {
foreach($filters as $filter) {
$query->orWhere(function($query) use ($filter) {
$query->where('product_options_pivot.options_id', $filter['option_id'])
->where('product_options_pivot.value_id', $filter['value_id']);
});
}
});
});
return $result->paginate(48);
}`
- Düzenlendi
cihanx Şöyle bir deneyin. | ile ayırdığınız değerlerde value solda option sağda demişsiniz, ben ters yapmışım, onu da düzelttim. Yazdığım şeyi direkt yapıştırıp çalışmadı demeyin, ne yaptığımı anlamaya çalışın, hata yapmış olabilirim, siz kendinize uyarlamaya çalışın.
$filters = [];
foreach ($request->query() as $key => $value) {
if (strpos($value, '|') !== false) {
$filter = explode('|', $value);
$filters[] = [
'option_id' => $filter[1],
'value_id' => $filter[0],
];
}
}
$result->when($filters, function ($query, $filters) {
$query->where(function ($query) use ($filters) {
$query->selectRaw('count(*)')
->from('product_options_pivot')
->whereColumn('product_options_pivot.product_id', 'product_category_pivot.product_id');
$query->where(function ($query) use ($filters) {
foreach ($filters as $filter) {
$query->orWhere(function ($query) use ($filter) {
$query->where('product_options_pivot.options_id', $filter['option_id'])
->where('product_options_pivot.value_id', $filter['value_id']);
});
}
});
}, '=', count($filters));
});
@mgsmus hocam, join yaptığım satırları ayırıp yukarıda kullandım ve filtre yapacağım bütün durumları da return paginate satırı arasında bıraktım. Şu an ne gelirse gelsin sorunsuz şekilde sonuç veriyor. Bilgi vermek amaçlı sizi etiketledim. Çok teşekkür ederim yardımlarınız için faydası oldu. Saygılarımla