2016-04-16 7 views
1

私の製品ページには類似した商品が表示されています。表示されている製品のbrand_idまたはcat_idと似ているすべての製品を検索するクエリがあります。私の問題は、同様のセクションで表示されている現在の製品も表示することです。私はそれを作る必要があるので、類似の製品セクションから見ている現在の製品を削除します。IDは閲覧中の現在の商品IDと一致しません - Laravel 5.2

これは私が今行っている質問です。私は ********( 'ID'、 '!=='、$製品 - > IDの一部が動作していない)

/** 
    * Show a Product in detail 
    * 
    * @param $product_name 
    * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View 
    */ 
    public function show($product_name) { 

     // Find the product by the product name in URL 
     $product = Product::ProductLocatedAt($product_name); 

     // Select All from "products" table where the brand_id is = to the current product being viewed with its brand_id, OR where 
     // the category_id is = to the current product category Id being viewed. This is so we can display similar products for a   // particular product being shown. 
     $similar_product = Product::where('brand_id', '=', $product->brand_id) 
       ->where('id', '!==', $product->id) 
       ->orWhere('cat_id', '=', $product->cat_id)->get(); 

     return view('pages.show_product', compact('product', 'similar_product')); 
    } 

******** EDITクエリメソッドを使用しているときにこれを取得する:

Undefined あなたはそうかもしれませんか?

+0

'!=='は有効な演算子ではありません。 '!='を試してください。 – patricus

答えて

2

Laravelのクエリビルダが追加しない場合デフォルトでは括弧です。

https://laravel.com/docs/5.2/queries

あなたのクエリは次のようになってしまいます。そのためORの

SELECT * 
FROM products 
WHERE brand_id = 1 
    AND id != 2 
    OR cat_id = 3 

、結果はそのCAT_IDに基づいて製品が含まれます。

あなたはおそらくしたいことは次のとおりです。、多くの場合、MySQLはで悪い仕事をしていません

SELECT * 
FROM products 
WHERE id != 2 
    AND (brand_id = 1 OR cat_id = 3) 

心に留めておいてください:

$similar_product = Product::where('id', '!=', $product->id) 
    ->where(function ($query) use ($product) { 
     $query->where('brand_id', '=', $product->brand_id) 
      ->orWhere('cat_id', '=', $product->cat_id); 
    })->get(); 

この括弧のセット内または一部を配置しますOR句で最適化します。テーブルが大きい場合は、パフォーマンスとインデックスの使用状況を再確認することができます。

+0

私の質問を最後にしました – David

+0

おっと、申し訳ありませんが、あなたに 'uses($ product)'が必要です。閉鎖。私は編集します。 –

+0

申し訳ありませんが、いくつかのエラーがあります、私は一番下に私の質問を編集しました。 – David

0

!==を<>に変更しようとしましたか?

/** 
* Show a Product in detail 
* 
* @param $product_name 
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View 
*/ 
public function show($product_name) { 

    // Find the product by the product name in URL 
    $product = Product::ProductLocatedAt($product_name); 

    // Select All from "products" table where the brand_id is = to the current product being viewed with its brand_id, OR where 
    // the category_id is = to the current product category Id being viewed. This is so we can display similar products for a   // particular product being shown. 
    $similar_product = Product::where('brand_id', '=', $product->brand_id) 
      ->where('id', '<>', $product->id) // changed this line.. 
      ->orWhere('cat_id', '=', $product->cat_id)->get(); 

    return view('pages.show_product', compact('product', 'similar_product')); 
} 

!==はMySQLの正しい構文ではありません。

+0

それを試して、動作しません、私は多分上記のカリーナのように、私の声明を並べ替える必要があります。 – David

0

カテゴリは、その句が声明

あなたの全体のためにtrueを返します一致しない場合、定義された優先順位はありませんので、論理的にあなたが

Brand ID equals AND product is not the same OR category equals 

を言っているので、私はそれが動作していないと思いますクエリをグループ化する方法を理解する必要があります。

(ID is not the same) AND (brand matches OR category matches) 
+0

ああ、私はそれを確定的に並べ替える必要があります – David

+0

並べ替えだけではありません。それを別々にグループ化する必要があります。おそらくこの回答を確認してください。http://stackoverflow.com/questions/18660180/laravel-or-where – karina

関連する問題