php - How to query Products according to the size? -
so have model product must filtered color, price, , size. here relationships.
class product extends model { public function color() { return $this->belongsto('app\color'); } public function sizes() { return $this->belongstomany('app\size', 'product_size')->withtimestamps(); } }
here size model:
class size extends model { public function products() { return $this->belongstomany('app\product', 'product_size')->withtimestamps(); } }
here form:
<form id="filteroptions" method="get" action="{{ url::current() }}"> <button type="submit" class="btn-color">filter</button> <div class="clearfix space20"></div> <h5>color</h5> <ul class="color-list"> @foreach($availablecolors $color) <li><input type="checkbox" name="color[]" value="{{ $color->id }}"><a href="#"><span class="{{ $color->name }}"></span> {{$color->name_bg}}</a></li> {{ $color->name }} @endforeach </ul> <div class="clearfix space20"></div> <h5>price</h5> <div id="slider-container"></div> <p> <span class="{{--pull-right--}} sc-range"> <input class="pull-left" name="min" type="text" id="min" style="border: 0; color: #333333; font-weight: bold;"/> <input class="pull-right" name="max" type="text" id="max" style="border: 0; color: #333333; font-weight: bold;"/> </span> </p> <div class="clearfix space30"></div> <h5>size</h5> <ul class="size-list"> @foreach($availablesizes $size) <li><input type="checkbox" name="size[]" value="{{ $size->id }}">{{ $size->size }}</li> @endforeach </ul> </form>
i manage query products price , color without problems. can't query when many many relationship. how can that? here code far:
$minprice = $request['min']; $maxprice = $request['max']; $colors = $request['color']; $sizes = $request['size']; if (count($request->all()) != 0) { $query = product::with(['sizes' => function($query) use($request) { $sizearray = $request->get('size'); $query->wherein('size', $sizearray); }]); if(isset($minprice) && isset($maxprice)) { $query->wherebetween('price', array($minprice, $maxprice)); } if(isset($colors)) { $query->wherein('color_id', $colors); } $products = $query->get(); }
everything works besides query sizes. how can fix this? please, can't find solution this.
Comments
Post a Comment