Php 在laravel 5中处理复杂查询

Php 在laravel 5中处理复杂查询,php,laravel,Php,Laravel,我有一个简单的任务和复杂的查询,50%已经完成了,剩下的我需要帮助 逻辑 我有登录页,在那里我选择类别,然后选择 该类别下的规格以及与这两个类别相关的产品 过滤器将显示在登录页中此操作已完成 我还想有登录页时,我选择类别没有 选择任何规格,它会显示与此相关的所有产品 类别,而不管他们有什么规格需要此帮助 代码 这是我的控制器为了更好地理解,我对每个部分都进行了注释 public function landings($slug){ $landing = Landing::where('sl

我有一个简单的任务和复杂的查询,50%已经完成了,剩下的我需要帮助

逻辑
  • 我有登录页,在那里我选择类别,然后选择 该类别下的规格以及与这两个类别相关的产品 过滤器将显示在登录页中<代码>此操作已完成
  • 我还想有登录页时,我选择类别没有 选择任何规格,它会显示与此相关的所有产品 类别,而不管他们有什么规格<代码>需要此帮助
  • 代码 这是我的控制器
    为了更好地理解,我对每个部分都进行了注释

    public function landings($slug){
        $landing = Landing::where('slug', $slug)->Status('Active')->firstOrFail(); //find landing page
    
        //getting landing pages category id (in case one landing page have several category involved)
        $cat = DB::table('landing_categories')->where('landing_id', $landing->id)->get();
        foreach ($cat as $key) {
          $id[] = $key->category_id;
        }
    
        // i just add this to filter landing without specifications for if statement below **not sure about this yet**)
        $spac = DB::table('landing_specifications')->where('landing_id', $landing->id)->get();
    
        // this is where things happen (first $prod is responsible for landing with specifications and working fine, **else part** need fix to show landings without specifications.
        if($spac != null){
          //old
          $prod = DB::table('landing_specifications')->where('landing_id', $landing->id)
          ->join('product_subspecification', function ($keys) {
              $keys->on('product_subspecification.subspecification_id', '=', 'landing_specifications.subspecification_id');
          })
          ->join('products', 'products.id', '=', 'product_subspecification.product_id')
          ->whereIn('products.category_id', $id)
          ->groupby('products.id')
          ->paginate(12);
        }else{
          //new
          $prod = DB::table('landing_categories')->where('landing_id', $landing->id)
          ->join('products', 'products.category_id', '=', 'landing_categories.category_id')
          ->whereIn('products.category_id', $id)
          ->groupby('products.id')
          ->paginate(12);
        }
    
    
        return view('front.landing', compact('landing', 'prod'));
      }
    
    希望我在上面代码中的评论对您有用,以避免误解

    附言:我知道我这里有两个主要问题

  • 我的if语句不对
  • 我的else部分需要修复(但在修复if()之前,我无法看到else部分的结果)
  • 有什么想法吗?

    解决了 我将if部分改为下面的代码,现在它工作得很好

    $spac = DB::table('landing_specifications')->where('landing_id', $landing->id)->first();
    
    if($spac){
    // rest of it...
    
    希望能有帮助