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.

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

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改