Laravel中表单输入值传递至另一页面失败求助
Hey Aga, let's break down the most common reasons your form data isn't showing up on show.blade.php—since there are no errors, it’s almost certainly a gap in how data flows from your form to the final view. Let’s go through each step to fix this:
First, make sure your web.php routes are set up correctly for the create → store → show flow. You need three routes: one to display the form, one to handle the form submission, and one to show the data. Example:
// web.php use App\Http\Controllers\AccController; Route::get('/accounts/create', [AccController::class, 'create'])->name('accounts.create'); Route::post('/accounts', [AccController::class, 'store'])->name('accounts.store'); Route::get('/accounts/{account}', [AccController::class, 'show'])->name('accounts.show');
- The
POSTroute (accounts.store) handles form submissions. - The
showroute uses route model binding (the{account}parameter) to fetch your model automatically—this is the cleanest way to pass data to the show view.
create.blade.php Ensure your form is submitting to the correct route, uses the POST method, and includes the CSRF token (required for Laravel POST requests). Also, double-check your input field name attributes—this is how Laravel reads the data:
<!-- create.blade.php --> <form method="POST" action="{{ route('accounts.store') }}"> @csrf <!-- Don't forget this! --> <div> <label for="username">Username:</label> <input type="text" id="username" name="username" required> </div> <!-- Add other form fields here, each with a unique `name` attribute --> <button type="submit">Submit</button> </form>
If your fields don’t have name attributes, Laravel won’t receive any data from the form.
store Method in AccController.php The store method needs to accept the form data, validate it (optional but recommended), save it to the database (if you’re using a model), then redirect to the show route with the data. Example:
// AccController.php use App\Models\Acc; // Replace with your actual model name use Illuminate\Http\Request; public function create() { return view('accounts.create'); // Make sure this matches your view path } public function store(Request $request) { // Validate the incoming data (adjust rules to match your fields) $validated = $request->validate([ 'username' => 'required|string|max:255', // Add validation rules for other fields ]); // Save the data to the database $account = Acc::create($validated); // Redirect to the show page, passing the saved account model return redirect()->route('accounts.show', $account); }
If you don’t want to save to the database yet (for testing), you can flash the data to the session instead:
return redirect()->route('accounts.show')->with('formData', $request->all());
show Method in AccController.php If you’re using route model binding, the show method will automatically receive your model instance. Just pass it to the view:
public function show(Acc $account) { // Optional: Debug to check if data is coming through // dd($account); return view('accounts.show', compact('account')); }
If you used session flashing instead, fetch the data from the session:
public function show() { $formData = session('formData'); return view('accounts.show', compact('formData')); }
show.blade.php Finally, make sure you’re correctly outputting the data in your view. For a model instance:
<!-- show.blade.php --> <h1>Account Details</h1> <p><strong>Username:</strong> {{ $account->username }}</p> <!-- Output other fields using $account->your_field_name -->
For session-flashed data:
<h1>Submitted Data</h1> <p><strong>Username:</strong> {{ $formData['username'] ?? 'No username provided' }}</p> <!-- Use the ?? operator to handle cases where data might be missing -->
- In the
storemethod, adddd($request->all());right after the method starts—this will show you exactly what data Laravel is receiving from the form. If it’s empty, your form’snameattributes are missing. - In the
showmethod, adddd($account)ordd($formData)to confirm the data is being passed to the view.
Once you fix these steps, your form data should show up on the show page! Let me know if you hit any snags along the way.
内容的提问来源于stack exchange,提问作者agawmd

