Laravel Blade调用路由时出现Undefined variable $userId错误求助
The error occurs because when you first load the form page, the $userId variable isn’t present in the view context. Your route expects {userId} as a URL parameter, but the form tries to pass this variable before the user has even entered an ID.
Solution 1: Submit User ID as Query Parameter (Simplest Fix)
Adjust the form to send the user ID as a query string instead of embedding it in the route URL. This eliminates the need for a pre-existing $userId variable when loading the form.
1. Update the Form
Replace your form code with this:
<form action="{{ route('booking.GetUserBookings') }}" method="get"> <input type="text" name="userId" placeholder="Enter User ID" required> <button type="submit">View Bookings</button> </form>
2. Modify the Route
Remove the {userId} parameter from your route definition:
Route::get('/booking/GetUserBookings', [BookingController::class, 'GetUserBookings'])->name('booking.GetUserBookings');
3. Keep Controller Logic
Your existing controller method already retrieves the ID from the request, so no changes are needed here:
public function GetUserBookings(Request $request) { $userId = $request->input('userId'); $bookings = DB::select('CALL GetUserBookings(?)', [$userId]); // Pass $userId back to pre-fill the input after submission (optional) return view('bookings.index', compact('bookings', 'userId')); }
Solution 2: Keep Clean URL Structure (Redirect Approach)
If you want to retain URLs like /booking/GetUserBookings/3, use a two-step submission flow:
1. Add a Search Route
Create a new route to handle form submissions, then redirect to your existing route with the user ID:
Route::get('/booking/search', [BookingController::class, 'SearchUserBookings'])->name('booking.SearchUserBookings'); Route::get('/booking/GetUserBookings/{userId}', [BookingController::class, 'GetUserBookings'])->name('booking.GetUserBookings');
2. Add Search Controller Method
public function SearchUserBookings(Request $request) { $userId = $request->input('userId'); return redirect()->route('booking.GetUserBookings', ['userId' => $userId]); }
3. Update the Form
Point the form to the new search route (no $userId variable required):
<form action="{{ route('booking.SearchUserBookings') }}" method="get"> <input type="text" name="userId" placeholder="Enter User ID" required> <button type="submit">View Bookings</button> </form>
4. Keep Original Controller Method
Your existing method that accepts the $userId parameter works as-is:
public function GetUserBookings($userId) { $bookings = DB::select('CALL GetUserBookings(?)', [$userId]); return view('bookings.index', compact('bookings', 'userId')); }
Why This Works
Both approaches eliminate the need for a $userId variable when the form first loads, fixing the undefined error. After submission, the user ID is passed to your controller either via query string or URL parameter, which you use to fetch the relevant bookings via your stored procedure.
内容的提问来源于stack exchange,提问作者The Devil

