duandaiqin6080 2016-06-18 21: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 21: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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line