duandaiqin6080 2016-06-18 13:26
浏览 51
已采纳

在搜索中添加一个数字以在php / laravel中显示正确的结果

I have created a simple search function that works by searching my required fields to match my keywords. This works fine for every column apart from my "Id" column, because when I display my ID in the table I actually add + 500 and some text. This is basically because the ID column is my booking/order reference and I didn't want to start at 0 and felt it easier to just offset the displayed result.

Basically I need it to add 500 to the search if when checking against the request_id column. So for the first record in the table would be 1 + 500 + my prefix which displays as SP-501. It I search for 501, it actually brings up record 501 in the table and not the correct one.

Heres my code:

public function search()
{

    $search = \Request::get('search'); 

    $bookings = LcsBooking::where('request_email','like','%'.$search.'%')
        ->orWhere('request_id', 'like', '%'.$search.'%')
        ->orWhere('request_name', 'like', '%'.$search.'%')
        ->orWhere('request_arrival', 'like', '%'.$search.'%')
        ->orWhere('request_return', 'like', '%'.$search.'%')
        ->orWhere('request_txauthno', 'like', '%'.$search.'%')
        ->orWhere('request_vehicle_reg', 'like', '%'.$search.'%')
        ->orWhere('request_mobile', 'like', '%'.$search.'%')
        ->orderBy('request_id', 'DESC')
        ->paginate(20);

    return view('admin.lcs.search',compact('bookings'));
}
  • 写回答

1条回答 默认 最新

  • dshtze500055 2016-06-18 13:59
    关注

    You could just make a check before to see if it's a string integer, and if so, add 500 to it.

    $search = \Request::get('search'); 
    $id = ctype_digit($search) ? $search + 500 : $search;
    
    $bookings = LcsBooking::where('request_email','like','%'.$search.'%')
        ->orWhere('request_id', 'like', '%'.$id.'%')
        ->orWhere('request_name', 'like', '%'.$search.'%')
        // and so on...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 点云密度大则包围盒小
  • ¥15 nginx使用nfs进行服务器的数据共享
  • ¥15 C#i编程中so-ir-192编码的字符集转码UTF8问题
  • ¥15 51嵌入式入门按键小项目
  • ¥30 海外项目,如何降低Google Map接口费用?
  • ¥15 fluentmeshing
  • ¥15 手机/平板的浏览器里如何实现类似荧光笔的效果
  • ¥15 盘古气象大模型调用(python)
  • ¥15 传人记程序做的plc 485从机程序该如何写
  • ¥15 已知手指抓握过程中掌指关节、手指各关节和指尖每一帧的坐标,用贝塞尔曲线可以拟合手指抓握的运动轨迹吗?