【问题标题】:laravel query return Undefinedlaravel 查询返回未定义
【发布时间】:2020-07-15 21:51:44
【问题描述】:

我用 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

变量正常接收值

【问题讨论】:

  • 第 4 行的 typeSearch 应该有 $ 符号,还要检查哪一行错误是指的
  • 是的,但一直给我发送未定义的变量:

标签: sql laravel where-clause


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-22
    • 2021-08-04
    • 2019-01-20
    • 1970-01-01
    • 2021-08-25
    • 2018-04-30
    • 2015-05-09
    • 1970-01-01
    相关资源
    最近更新 更多