Michael O'Connor Michael O'Connor - 4 months ago 4x
MySQL Question

Adding a number to a search to display correct result in php/laravel

I'm new to PHP and Laravel but really enjoying the learning experience. This is my first every question on here, so please bare with me if I don't make much sense to you veteran coders out there.

My problem:

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')

return view('admin.lcs.search',compact('bookings'));


Any help getting me on the right track would be greatly appreciated.

Thanks :)


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...