It turns out that here, using PostgreSQL, it works differently from your database when I do this:
Route::any('test', function() { $code = '181rerum'; return Ad::where('id', $code)->orWhere('company_code', $code)->first(); });
I get this error:
SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "181rerum" (SQL: select * from "ads" where "id" = 181rerum or "company_code" = 181rerum limit 1)
So, Laravel, knowing that this is an integer column, passes it directly to the database without quotes, which throws a database exception, because PostgreSQL does not even try to use this row for an integer.
So, even if you get some help from the main Laravel developers, I think you should always do something like this to help you in these mixed situations:
Route::any('test/{id}', function($id) { /// You can always filter by a string here $q = Ad::where('company_code', $id); /// You just try to filter by id if the search string is entirely numeric if (is_numeric($id)) { $q->orWhere('id', $id); } return $q->first(); });
source share