首页 > 解决方案 > laravel 查询返回未定义

问题描述

我用 3 个输入进行搜索,然后输入可以为空

我从前端获取变量

$addressSearch = $request->json()->get('addressSearch');
$typeSearch    = $request->json()->get('typeSearch');
$statusSearch  = $request->json()->get('statusSearch');

我使用查询中的函数进行此搜索

$property = Property::where('id', '1')
                      ->Where(function($query)
                       {
                           $query->Where('type',typeSearch)
                                   ->orWhere('status',$statusSearch)
                                   ->orWhere('street','LIKE',"% $addressSearch}%");
                        })
                        ->get();

我得到这个错误:

消息:“未定义的属性:App\Http\Controllers\PropertyController::$typeSearch

变量正常接收值

标签: sqllaravelwhere-clause

解决方案


您有类型错误,并且您没有将任何变量传递给闭包函数,因此它们无论如何都不存在:

$property = Property::where('id', '1')
                     ->where(function($query) use ($typeSearch, $statusSearch, $addressSearch)
                     {
                          $query->where('type',$typeSearch)
                                ->orWhere('status',$statusSearch)
                                ->orWhere('street','LIKE',"%{$addressSearch}%");
                     })->get();

推荐阅读