Hello.
I cannot get the "many to many" relation working on product filtering. I would just greatly appreciate if someone would help me learn, how it is supposed to be set up.
I have an array of category id's but i cannot figure out, the "Laravel way" to do this right.
Thank you so much in advance.
My DB schema:
ads | categories | ad_category_rels
My controller:
$query = DB::table('ads');
if($request->gender) {
$gender = $request->gender;
if ($gender != 0) {
$query->where("gender", "=", "$gender");
}
}
if($request->min_age) {
$min_age = $request->min_age;
$query->where("from_year", ">=", "$min_age");
}
if($request->max_age) {
$max_age = $request->max_age;
$query->where("to_year", "<=", "$max_age");
}
if($request->min_price) {
$min_price = $request->min_price;
$query->where("price", ">=", "$min_price");
}
if($request->max_price) {
$max_price = $request->max_price;
$query->where("price", "<=", "$max_price");
}
if($request->sort_order) {
$sort_order = strtoupper($request->sort_order);
} else {
$sort_order = 'DESC';
}
if($request->sort) {
$sort = $request->sort;
$query->orderBy("$sort", $sort_order);
}
if($request->type) {
$type = $request->type;
if($type == 'digitale' || $type == 'digital') {
$type = '1';
} else if ($type == 'fysiske' || $type == 'physical') {
$type = '2';
}
$query->where("type", "=", "$type");
}
if($request->categories) {
$categories = explode(",", $request->categories);
foreach ($categories as $category) {
// Category ids
}
}
$limit = 5;
$page = $request->page;
$offset = ($page * $limit) - $limit;
$query->offset($offset);
$query->limit($limit);
$ads = $query->paginate($limit);
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community