Laravelで商品を属性(色、サイズなど)でフィルタリングする方法
P粉821274260
P粉821274260 2023-08-28 00:17:09
0
2
536
<p>Laravel を初めて使用するので、特定の製品を除外したいと考えています。 </p> <p>データベースに 2 つのテーブルがあります。1 つ目はテーブル <code>products</code>、2 つ目はテーブル <code>attributes</code> です。 </p> <p><strong>商品リスト</strong></p> <pre class="brush:php;toolbar:false;">Schema::create('products', function (Blueprint $table) { $table->bigIncrements('id'); $table->BigInteger('category_id')->unsigned()->nullable(); $table->string('name'); $table->string('code'); $table->integer('status')->default(1); $table->integer('featured')->default(1); $table->string('画像'); $table->longText('short_description'); $table->longText('long_description'); $table->timestamps(); })</pre> <p><strong>製品属性テーブル</strong></p> <pre class="brush:php;toolbar:false;">Schema::create('product_attributes', function (Blueprint $table) {. $table->bigIncrements('id'); $table->unsignedBigInteger('product_id'); $table->string('sku'); $table->string('size'); $table->string('color'); $table->string('価格'); $table->string('ストック'); $table->timestamps(); })</pre> <p><strong>人間関係</strong></p> <p>単一の商品に複数の属性があるため</p> <pre class="brush:php;toolbar:false;">クラス プロダクトはモデルを拡張します { HasFactoryを使用します。 パブリック関数の属性() { return $this->hasmany('App\Models\ProductAttributes', 'product_id'); } }</pre> <p><strong>マイ ブレード ファイル</strong></p> <pre class="brush:php;toolbar:false;"><form action="{{url('/product/filter')}}"method="post"> @csrf <入力タイプ="隠し"値="{{$slug}}"名前="ナメクジ">
<label class="custom-control-label" for="black">Black</label> </div> </form></pre> <p>コントローラーに関数があります</p> <pre class="brush:php;toolbar:false;">パブリック関数 shop() { $filter_products = Product::with('attributes')->where(['category_id' => $category->id, 'color' => $request->color]); return view('frontend.shop', Compact('filter_products')); }</pre> <p>この関数を適用しても結果が得られません</p> <p>フロントエンド ストア ページで特定のサイズや色に基づいて商品をフィルタリングする方法を教えてください。 そして、ストア機能にどのようなコードが含まれるか。 </p> <p>返信してください。よろしくお願いします</p>
P粉821274260
P粉821274260

全員に返信(2)
P粉908138620
$brands         = Brand::orderBy('id','DESC')->get();
    $colors         = Color::orderBy('id','DESC')->get();
    $sizes          = Size::orderBy('id','DESC')->get();
    $weights        = Weight::orderBy('id','DESC')->get();
    $tags           = Tag::orderBy('id','DESC')->get();
    try{
        if (!empty($_GET['colors']) || !empty($_GET['brands']) || !empty($_GET['sizes']) || !empty($_GET['weights']) || !empty($_GET['tags'])) {
            $products = Product::where( function($query ){
                $query->when(!empty(request()->colors) ,function($subquery){
                    $subquery->whereHas('colors', function($subquery)  {
                        $subquery->whereIn('colors.id',request()->colors);
                    });
                })->when(!empty(request()->sizes) ,function($subquery){
                    $subquery->whereHas('sizes', function($subquery)  {
                        $subquery->whereIn('sizes.id',request()->sizes);
                    });
                })->when(!empty(request()->weights) ,function($subquery){
                    $subquery->whereHas('weights', function($subquery)  {
                        $subquery->whereIn('weights.id',request()->weights);
                    });
                })->when(!empty(request()->tags) ,function($subquery){
                    $subquery->whereHas('tags', function($subquery)  {
                        $subquery->whereIn('tags.id',request()->tags);
                    });
                })->when(!empty(request()->brands) ,function($subquery){
                    $subquery->whereHas('brand', function($subquery)  {
                        $subquery->whereIn('brand_id',request()->brands);
                    });
                })->when(!empty(request()->is_new) ,function($subquery){
                    $subquery->where('is_new',request()->is_new);
                })->when(!empty(request()->gender) ,function($subquery){
                    $subquery->where('gender', 'LIKE', "%" . request()->gender . "%");
                });
            })->paginate(10);
        
            
            return view('front.shop',compact('products','brands','colors','sizes','weights','tags'));
        } else {
            return  redirect()->route('products');
        }
        
    } catch (\Exception $e) {
        toastr()->error(trans('Some thing is wrong please try again'));
        return  redirect()->route('products');
    }
いいねを押す +0
P粉033429162

関係でフィルタリングする必要があります。ドキュメントを確認してください

https://laravel.com/docs/9 .x/eloquent-relationships#querying-relationship-existence

###例えば WhereHas

を使用する リーリー

どこにも適用されない場合、すべてのプロパティが返されます

whereHas に適用される同じフィルターを使用して、この動作を防ぐことができます

リーリー

いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!