sql - 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
变量正常接收值
解决方案
您有类型错误,并且您没有将任何变量传递给闭包函数,因此它们无论如何都不存在:
$property = Property::where('id', '1')
->where(function($query) use ($typeSearch, $statusSearch, $addressSearch)
{
$query->where('type',$typeSearch)
->orWhere('status',$statusSearch)
->orWhere('street','LIKE',"%{$addressSearch}%");
})->get();
推荐阅读
- html - 向左浮动 div 100% 宽度
- random - Generate random float from Standard Normal distribution and multiply by another float
- matlab - Create surface plot in MATLAB using x,y,z array
- rust - 尝试变异时 Rust atomic 没有警告
- javascript - 键入一个接受组件引用的属性/变量?
- javascript - 无服务器、API Rest、在 Lambda POST 上获取正文
- python - python中的lambda函数是必需的还是只是选项快捷方式?
- firebase-realtime-database - 实时数据库部分中的firebase控制台不会停止加载?
- android - 脉冲音频的遥控器?
- java - 如何在 Spring Boot 中使用 Runnable 和 mockito