Laravel视图间传值:点击Edytuj按钮传递信息并关联ID
Let's break down how to get your "Edytuj" (Edit) button working to pass presentation data, and ensure all new slides are linked to that presentation's ID. Here's what you need to do:
1. Update Your SlideController to Accept the Presentation ID
Right now, your slideindex method doesn't use the ID parameter from the route. Let's fix that to fetch the presentation details and its associated slides:
// SlideController.php public function slideindex($presentationId) { // Fetch the presentation to get its name (assuming you have a Presentation model) $presentation = Presentation::findOrFail($presentationId); // Get only slides belonging to this presentation $slidesList = Slide::where('presentation_id', $presentationId)->get(); return view('slide-list', [ "slidesList" => $slidesList, "title" => "Slides index", "presentationId" => $presentationId, "presentationName" => $presentation->name ]); }
Note: Make sure your Slide model has a presentation_id foreign key column in its database table, and your Presentation model exists with a name field.
2. Update the Target Blade View (slide-list.blade.php)
Now we'll display the dynamic presentation name, and link the "Dodaj slajd" (Add Slide) button to a route that carries the presentation ID:
@extends('template') @section('title'){{ $title }} @endsection @section('content') <nav class="navbar fixed-top navbar-expand-lg navbar-light bg-light"> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav"> <li class="nav-item"> <a class="nav-link" href="/index">Index <span class="sr-only">(current)</span></a> </li> <li class="nav-item"> <!-- Link to create slide with presentation ID --> <a class="nav-link" href="{{ route('slide.create', ['presentationId' => $presentationId]) }}">Dodaj slajd</a> </li> <li class="nav-item"> <!-- Display dynamic presentation name --> <span class="navbar-brand">{{ $presentationName }}</span> </li> </ul> </div> </nav> <div class="container"> <table class="table table-hover"> <thead> <tr> <th>Id</th> <th>Nazwa</th> <th>Typ</th> <th>Akcja</th> </tr> </thead> <tbody> @foreach ($slidesList as $slide ) <tr> <th scope="row">{{$slide->id}}</th> <td>{{$slide->slideName}}</td> <td>{{$slide->type}}</td> </tr> @endforeach </tbody> </table> </div> @endsection
3. Add Routes for Creating/Storing Slides
Update your web.php routes to include named routes for slide creation (this makes linking easier):
// web.php // Keep your existing routes, then add these: Route::get('index/edit/{ID}','SlideController@slideindex')->name('slide.index'); Route::get('index/slide/create/{presentationId}', 'SlideController@create')->name('slide.create'); Route::post('index/slide/store', 'SlideController@store')->name('slide.store');
4. Add Slide Creation Logic to SlideController
Create methods to show the add slide form and save the new slide with the correct presentation ID:
// SlideController.php public function create($presentationId) { $presentation = Presentation::findOrFail($presentationId); return view('slide-create', [ 'presentationId' => $presentationId, 'presentationName' => $presentation->name ]); } public function store(Request $request) { // Validate input $request->validate([ 'slideName' => 'required|string', 'type' => 'required|string', 'presentation_id' => 'required|exists:presentations,id' ]); // Create new slide linked to the presentation Slide::create([ 'slideName' => $request->slideName, 'type' => $request->type, 'presentation_id' => $request->presentation_id ]); // Redirect back to the slide list for this presentation return redirect()->route('slide.index', ['ID' => $request->presentation_id]); }
5. Create the Slide Creation View (slide-create.blade.php)
Make a simple form to add new slides, hidden input to carry the presentation ID:
@extends('template') @section('title')Dodaj Slajd @endsection @section('content') <div class="container mt-5"> <h2>Dodaj slajd dla prezentacji: {{ $presentationName }}</h2> <form method="POST" action="{{ route('slide.store') }}"> @csrf <input type="hidden" name="presentation_id" value="{{ $presentationId }}"> <div class="form-group"> <label for="slideName">Nazwa slajdu</label> <input type="text" class="form-control" id="slideName" name="slideName" required> </div> <div class="form-group"> <label for="type">Typ slajdu</label> <input type="text" class="form-control" id="type" name="type" required> </div> <button type="submit" class="btn btn-primary">Zapisz slajd</button> </form> </div> @endsection
6. Update the "Edytuj" Button in Your Source View
Wherever your "Edytuj" button lives (probably in your DataController@index view), link it to the named route with the presentation ID:
<!-- Example in your presentation list view --> <a href="{{ route('slide.index', ['ID' => $presentation->id]) }}" class="btn btn-warning">Edytuj</a>
Final Checks
- Ensure your
Slidemodel has fillable attributes:class Slide extends Model { protected $fillable = ['slideName', 'type', 'presentation_id']; } - Add a relationship to your
Presentationmodel if needed:class Presentation extends Model { public function slides() { return $this->hasMany(Slide::class); } }
This setup will pass the presentation ID when you click "Edytuj", display the correct presentation name in the slide view, and ensure all new slides are assigned to that presentation's ID automatically.
内容的提问来源于stack exchange,提问作者buhh1231

