You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

Step 1: Verify Your Route Configuration

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 POST route (accounts.store) handles form submissions.
  • The show route uses route model binding (the {account} parameter) to fetch your model automatically—this is the cleanest way to pass data to the show view.
Step 2: Check Your Form Setup in 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.

Step 3: Fix the 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());
Step 4: Update the 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'));
}
Step 5: Render Data in 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 -->
Quick Debugging Tips
  • In the store method, add dd($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’s name attributes are missing.
  • In the show method, add dd($account) or dd($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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:00:46