dongsong73032 2017-02-23 23:23
浏览 104
已采纳

如何在laravel 5.4中将多个范围添加到DB :: table

I am new on laravel 5, I was trying to add multiples scopes like this:

$projects = DB::table('projects AS p')->select('p.project_name','c.name_country', 'p.date_project','s.name_status','p.rejected_date')                    
                    ->join('country AS c','c.id','=','p.id_country')
                    ->join('status AS s','s.id','=','p.id_status')
                    ->where('p.active','=','1');  

$projects = $this->scopeCountry($projects , $country);
$projects = $this->scopeStatus($projects , $status);  
$projects->get(); 
return view('search.ajax')->with('projects', $projects);
}
    public function scopeCountry($query, $country){
            return is_null($country) ? $query : $query->where('p.id_country','=',$country);
        }

public function scopeStatus($query, $status){
            return is_null($status) ? $query : $query->where('p.id_status','=',$status);
        }
}

It gave me this error: ErrorException in 26864233b0cd26a1055b66c2645628be451523fd.php line 4: Undefined property: Illuminate\Database\MySqlConnection::$project_name (View:/home/vagrant/Code/projects/resources/views/search/ajax.blade.php)

I noticed that this error doesn't show up if I modify it like this:

//$projects = $this->scopeStatus($status);  
//$projects->get(); 

And then I add the get() function in:

public function scopeCountry($query, $country){
                return is_null($country) ? $query->get() : $query->where('p.id_country','=',$country)->get();
            }

The reason why I don't add the queries in one scope and I use is_null($country), is because in case those variables($country, $status) were null only the main query should be executed:

$projects = DB::table('projects AS p')->select('p.project_name','c.name_country', 'p.date_project','s.name_status','p.rejected_date')                    
                        ->join('country AS c','c.id','=','p.id_country')
                        ->join('status AS s','s.id','=','p.id_status')
                        ->where('p.active','=','1'); 

And also I have to add similar scopes to another fields, maybe there is another way to do it, I am not sure. Thank you.

  • 写回答

1条回答 默认 最新

  • duan198299 2017-02-27 17:26
    关注

    I just found out an easier solution using the when method:

    $projects = DB::table('projects AS p')->select('p.project_name','c.name_country', 'p.date_project','s.name_status','p.rejected_date')                    
                            ->join('country AS c','c.id','=','p.id_country')
                            ->join('status AS s','s.id','=','p.id_status')
                            ->where('p.active','=','1') 
                            ->when($country, function($query) use ($country){
                                 return $query->where('p.id_country','=',$country);
                            })
                            ->when($status, function($query) use ($status){
                                 return $query->where('p.id_status','=',$status);
                            }); 
    

    When the $country or $status variable is not present or null, what it is inside the closure doesn't execute.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?