dqqy64515 2018-09-21 17:19
浏览 84
已采纳

Laravel 5.6:如何制作App \ sys_data :: where($ moreWhere) - > paginate(10); 工作?

I have three different columns in my database. I want to allow user to fetch data from these columns based on his/her needs. The user could choose to fetch all data from all columns or the user could choose specific information about a column.

$moreWhere="";
if($request->status != 'all')
    $moreWhere="['status', '=', '$request->status'],";
if($request->category != 'all')
    $moreWhere = $moreWhere . "['cat', '=', '$request->cat'],";
if($request->type != 'all')
    $moreWhere = $moreWhere . "['type', '=', '$request->type']";

$moreWhere = '[' . $moreWhere . ']';

$showData = sys_data::where($moreWhere)->wherebetween('date', [$request->from_date, $request->to_date])->paginate(10);

The problem I have is that, when I pass the $moreWhere variable to "where" clause of the eqlouent, I get an error.

It gives me the following error.

Illuminate \ Database \ QueryException (42S22)
SQLSTATE[42S22]: Column not found: 1054 Unknown column 
'[['status', '=', 'Yes'],['cat', '=', 'Food'],['type', '=', 'Free']]' 
in 'where clause' (SQL: select count(*) as aggregate 
from `sys_data` where `[['status', '=', 'Yes'],['cat', '=', 'Food'],['type', '=', 'Free']]` = `` 
and `date` between 2018-09-01 and 2018-09-03)

If I manually add

[['status', '=', 'Yes'],['cat', '=', 'Food'],['type', '=', 'Free']]

as argument to "where" like following

sys_data::where([['status', '=', 'Yes'],['cat', '=', 'Food'],['type', '=', 'Free']])

then it works like a charm.

However, the same thing saved in a variable and assigned to "where", just doesn't work. Am I missing something?

I am sorry I am new to Laravel and programming, so any help would be greatly appreciated. Again, I am using Laravel 5.6, if that's relevant here.

Thanks

  • 写回答

2条回答 默认 最新

  • dongzong2017 2018-09-21 17:25
    关注

    If you want to dynamically add where statements to your query depending on other settings you can either start your query with a variable or add a closure inside a where call.

    Option 1:

    $query = sys_data::query();
    
    if($request->status != 'all')
        $query->where('status', $request->status);
    if($request->category != 'all')
        $query->where('cat', $request->cat);
    if($request->type != 'all')
        $query->where('type', $request->type);
    
    $query->wherebetween('date', [$request->from_date, $request->to_date])->paginate(10);
    
    $showData = $query;
    

    Option 2:

    $showData = sys_data::where(function($query){
        if($request->status != 'all')
            $query->where('status', $request->status);
        if($request->category != 'all')
            $query->where('cat', $request->cat);
        if($request->type != 'all')
            $query->where('type', $request->type);
    
        return $query;
    })->wherebetween('date', [$request->from_date, $request->to_date])->paginate(10);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法