Laravel中使用%LIKE%查询返回空数组问题求助
Hey there, let's walk through the most common reasons your $searchedLocations is coming up empty even though matching records exist in your database, and how to fix each one:
1. Verify the $searchKey is actually being passed correctly
First, let's confirm the controller is receiving the value you expect. Throw a quick debug check at the start of your method:
public function getLocationsList(Request $request,$searchKey) { dd($searchKey); // This will dump the value and stop execution // ... rest of your code }
- If the dumped value is empty or not what you typed in your frontend, the issue is with how Axios is sending the parameter:
- Double-check your route definition: If you're using a route parameter (like
Route::get('/locations/{searchKey}', ...)), your Axios URL must include the search term as part of the path, not a query string. For example:// Correct for route parameter axios.get(`/api/locations/${yourSearchTerm}`) // Wrong if using route parameter (this sends it as ?searchKey=...) axios.get('/api/locations', { params: { searchKey: yourSearchTerm } }) - If you intended to use a query string instead, adjust your controller to pull the value from the request:
$searchKey = $request->query('searchKey');
- Double-check your route definition: If you're using a route parameter (like
2. Check for case sensitivity in your LIKE query
Many databases (like MySQL) use collations that are case-sensitive by default for certain character sets. For example, if your location field has "Paris" stored but you're searching for "paris", a standard LIKE query might return nothing.
- Fix this by making the query case-insensitive:
$searchedLocations = Hotel::whereRaw('LOWER(location) LIKE ?', ["%".strtolower($searchKey)."%"])->get(); - Alternatively, you can modify your
locationcolumn's collation to a case-insensitive one (likeutf8mb4_unicode_ci) directly in your database.
3. Validate your raw SQL query against the database
Skip Laravel entirely for a minute and run the equivalent SQL directly in your database tool (phpMyAdmin, TablePlus, etc.):
SELECT * FROM hotels WHERE location LIKE '%your-search-term%';
- If this returns no results, either:
- The
locationfield doesn't actually contain the search term (maybe a typo in the field name, likelocationsinstead oflocation?) - Your
Hotelmodel is pointing to the wrong table: Verify the model has the correct table name defined:protected $table = 'hotels'; // Make sure this matches your actual table name
- The
4. Check for global scopes or hidden filters in your model
If you've added a global scope to the Hotel model (like filtering only active hotels), it might be excluding matching records without you realizing it. Test by temporarily disabling global scopes:
$searchedLocations = Hotel::withoutGlobalScopes()->where('location', 'LIKE', "%$searchKey%")->get();
If this returns results, you'll need to adjust your global scope to allow this query to bypass it, or modify the scope to include the records you need.
5. Ensure Axios isn't sending an empty or malformed search term
Double-check your Vue code to make sure yourSearchTerm isn't empty, trimmed incorrectly, or has extra whitespace. For example, if your input has leading/trailing spaces, the query will look for % paris % instead of %paris%. You can add a trim in your frontend:
const cleanedSearchTerm = yourSearchTerm.trim(); axios.get(`/api/locations/${cleanedSearchTerm}`)
内容的提问来源于stack exchange,提问作者Rakesh Kohali

