多个Laravel无法正常工作

时间:2017-04-04 06:27:18

标签: php laravel laravel-5 laravel-5.2

任何人都可以帮我修复此代码吗?

public function caritindaklanjut(Request $request){
    $halaman="tindaklayanan";
    $cari = '%'.$request->search.'%';
    $rows=DB::table('keluhans')
    ->select(DB::raw('id,produk,username,masalah,status,id_departemen'))
    ->where('status','=','Pending')
    ->where(function($query) {
    $query->where('produk','like',$cari)
        ->orWhere('username','like',$cari)
        ->orWhere('masalah','like',$cari);
    })->orderby('id','asc')->get();

它无法将$ cari传递给$ query并收到错误...请帮助:)

2 个答案:

答案 0 :(得分:3)

添加use($cari)

->where(function($query) use($cari) {

答案 1 :(得分:2)

你需要有一个"使用($ cari)"声明在第二个匿名函数where():

public function caritindaklanjut(Request $request){
    $halaman="tindaklayanan";
    $cari = '%'.$request->search.'%';
    $rows=DB::table('keluhans')
    ->select(DB::raw('id,produk,username,masalah,status,id_departemen'))
    ->where('status','=','Pending')
   ->where(function($query) use ($cari) {
        $query->where('produk','like',$cari)
        ->orWhere('username','like',$cari)
        ->orWhere('masalah','like',$cari);
    })->orderby('id','asc')->get();