 |
Laravel or MySQLi - Using Joins [Filter Categorie Product] |
|
 |
|
|
 |
 |
|
รบกวนสอบถามเรื่องเทคนิคการ Join Advance Filter Category Product ครับ
สมมุติ ตอนนี้หน้า blade แบ่งออกเป็น 3 mutiselect box(brand,color,size) ตอนนี้นำ 3 mutiselect box รวมเข้าไปในตัวแปรตัวเดียวชื่อ $filter_lists แล้วนำไป whereIn (tb:filter_options_products) ค่าเลยไม่ได้อย่างที่ต้องการ เมื่อ เลือก แบรนตัวนั้นแล้ว Product ที่อยู่ใต้ filter ตัวนั้นออกมาครับ แต่เมื่อเลือกสี สินค้าที่ไม่ใช่แบรนนั้นก็ออกมาเหมือนกัน ผมจะทำอย่างไรบ้างครับ
ขอเป็นแนวทางของ mysql join ก็ได้ครับ
#ขอบคุณครับ
Code (PHP)
$products=Product::
join('category_products','category_products.product_id','=','products.id')
->where('category_products.categoy_id',$request->category_id)
->join('filter_options_products','filter_options_products.product_id','=','products.id')
->whereIn('filter_options_products.filter_option_id',$request->filter_lists)
->select('products.*')
->get();

Tag : PHP, MySQL, Ajax, jQuery, Laravel Framework
|
ประวัติการแก้ไข 2019-12-08 00:17:55
|
 |
 |
 |
 |
Date :
2019-12-08 00:16:19 |
By :
Genesis™ |
View :
1129 |
Reply :
9 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (SQL)
SELECT * FROM `products`
INNER JOIN `category_products` ON `category_products`.`product_id` = `products`.`id`
INNER JOIN `filter_options_products` ON `filter_options_products`.`product_id` = `products`.`id`
WHERE `category_products`.`category_id` = 2
AND `filter_option_id` IN (1,2,3)
ผมลองแบบนี้มันก็ไม่ออกมานี่นา?
|
 |
 |
 |
 |
Date :
2019-12-08 10:30:46 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมไม่ค่อยชำนาญเรื่อง db ต้องยกให้คุณ @Chaidhanan
แต่ว่าทำไมไม่ลองใช้คำสั่งธรรมดา พวก ORM เมื่อก่อนผมเคยใช้กับตัว fuelphp มันก็เพี้ยนๆหน่อยๆเหมือนกัน พอเลาะมาเขียนคำสั่งธรรมดามันได้ผลตรงใจมากกว่า
|
 |
 |
 |
 |
Date :
2019-12-08 13:46:42 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แก้ไขปัญหาได้แล้วครับ แต่ขอบคุณสำหรับคำแนะนำนะครับ   
Code (PHP)
if(!isset($request->filter_lists))
{
$products=Product::
join('categorie_product','categorie_product.product_id','=','products.id')
->where('categorie_product.categorie_id',$request->categorie_id)
->select('products.*')
->get();
}else{
for ($i=0; $i < count($request->filter_lists); $i++) {
$product[$i]=Product::
join('categorie_product','categorie_product.product_id','=','products.id')
->where('categorie_product.categorie_id',$request->categorie_id)
->join('filter_product','filter_product.product_id','=','products.id')
->whereIn('filter_product.filter_id',$request->filter_lists[$i])
->select('products.*')
->get();
}
if(count($product) == 1)
{
$products=$product[0];
}else{
for ($i=0; $i < count($product) ; $i++) {
$arr[$i]=array();
for ($a=0; $a < count($product[$i]); $a++) {
array_push($arr[$i],$product[$i][$a]['id']);
}
}
for ($i=1; $i < count($arr); $i++) {
$arr[0]=array_intersect($arr[0], $arr[$i]);
}
$products=Product::whereIn('id',$arr[0])->get();
}
}
|
ประวัติการแก้ไข 2019-12-08 21:47:24 2019-12-09 09:58:04 2019-12-09 09:58:35
 |
 |
 |
 |
Date :
2019-12-08 21:46:19 |
By :
Genesis™ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ต้องการแสดง product ที่ตรงกันกับ filter 3 อย่าง
ตารางหลัก คือ product
ตารางรอง คือ filter
Code (SQL)
select pd.* from product as pd
left join filter as ft
on ft.pd_id = pd.id
and ft.category in ( ?1, ?2, ?3)
group by pd.id
having count(pd.id)=3
ปล filter คุณต้องเขียน sub query เอาเอง นะ
|
ประวัติการแก้ไข 2019-12-10 03:05:23
 |
 |
 |
 |
Date :
2019-12-10 02:58:48 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัว laravel ผมไม่ค่อยคล่อง (เอาเป็นไม่เป็นดีกว่า เคยอ่านแค่ครั้งสองครั้ง ยังไปไม่ถีงไหน)
3 listbox ต้องถามก่อนว่า ทั้ง 3 ตัว มันอ้าง table เดียวกันหรือไม่ แต่ดูจาก chart db ที่ลง มันคนละ ตาราง
มี ตารางที่เกี่่ยวข้อง ก็คือ ตาราง filters ตรงจุดนี้ ควรสร้าง views เอาไว้เลย เพื่อใช้ในการคิวรี่ สั้นๆ
Code (SQL)
### --- vFilter ---- View in database
select ft.*, dt.ft_Type, dt.ref_op_or_cg, dt.ref_for_category_id,
from filters as ft
inner join (
select 'cg' as ft_Type, fc.category_id as ref_op_or_cg, fc.filter_id as ft_ID, fc.id ref_for_category_id, cp.product_id
from filters_categories fc left join category_product cp on cp.category_id = fc.category_id
union all
select 'op' as ft_Type, id, filter_id, null, fop.product_id
from filter_options as fo left join filter_options_product fop on fop.fileter_option_id = fo.id
) as dt on dt.ft_ID = ft.id
Code (PHP)
$sql="select pd.* from products pd
left join vFilter v
on v.id = $request_filter_select_id ## อ้างอิงตาราง filters
and (
(
v.ft_Type = 'cg'
and
v.ft_ID in (".implode(',', $request_filter_category).")) ## อ้างอิง category
or
(
v.ft_Type = 'op'
and
v.ft_ID in (".implode(',', $request_filter_option).")) ## อ้างอิง option
)
and pd.id = v.product_id
group by pd.id having count(ft.*)=".(count($request_filter_category)+count($request_filter_option);
ปล.
ทั้งนี้ทั้งนั้น เดาเอาทั้งนั้นเพราะไม่รู้ว่า input มาจากไหน ตัว list_box เอามาจากอะไรบ้าง
|
ประวัติการแก้ไข 2019-12-11 12:13:50
 |
 |
 |
 |
Date :
2019-12-11 12:09:40 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|