'How do I keep the selected entry in the table and use it on the next view?
Good day all I have a table that shows some basic information about actors like name, surname, age and so on and on the right of each actor I have Three buttons edit, view, delete. My problem is that when I click the view button I need to know what entry in the table was selected to be viewed so that I can show the information. The data is stored inside a JSON file and is extracted through the controller and passed into the blade.php view to show in the table. This is the code used to show each actor in the table
<a class="btn btn-primary" href="/actor/create" role="button">Actors</a>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Age</th>
<th>Nationallity</th>
<th>Options</th>
</tr>
</thead>
<tbody>
<?php foreach($jsonArray as $actor):?>
<tr>
<td> {{$actor['actorName']}}</td>
<td> {{$actor['actorSurname']}}</td>
<td> {{$actor['actorAge']}}</td>
<td> {{$actor['actorNationality']}}</td>
</td>
<td>
<a href="/actor/edit" class="btn btn-primary a-btn-slide-text">
<span class="glyphicon glyphicon-edit" aria-hidden="true"></span>
<span><strong>Edit</strong></span>
</a>
<a href="/actor/show" class="btn btn-primary a-btn-slide-text">
<span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span>
<span><strong>View</strong></span>
</a>
<a href="#" class="btn btn-primary a-btn-slide-text">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
<span><strong>Delete</strong></span>
</a>
</td>
</tr>
<?php endforeach;;?>
</tbody>
</table>
So when the view is selected I need to know what actor so I can show the information of that actor to the user. And how do I pass it to the next view?
My web.php File
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/', function ()
{
return view('welcome');
});
Route::get ('actor/create',[ActorsController::class, 'createView']);
Route::post ('actor/create',[ActorsController::class, 'store'])->name('actor.store');
Route::get ('actor/edit',[ActorsController::class, 'edit']);
Route::get ('actor/index',[ActorsController::class, 'index']);
Route::get ('actor/show/{actorID}',[ActorsController::class, 'show']);
Route::get ("/director/create",[DirectorController::class, 'create']);
Route::post ('director/create',[DirectorController::class, 'store'])->name('director.store');
Route::get ("/director/edit",[DirectorController::class, 'edit']);
Route::get ("/director/index",[DirectorController::class, 'index']);
Route::get ("/director/show",[DirectorController::class, 'show']);
Route::get ("/genre/create",[GenreController::class, 'create']);
Route::post ("genre/create",[GenreController::class, 'store'])->name('genre.store');
Route::get ("/genre/edit",[GenreController::class, 'edit']);
Route::get ("/genre/index",[GenreController::class, 'index'])->name('genre.index');
Route::get ("/genre/show",[GenreController::class, 'show']);
Route::get ("/movie/create",[MovieController::class, 'create']);
Route::post ('movie/create',[MovieController::class, 'store'])->name('movie.store');
Route::get ("/movie/edit",[MovieController::class, 'edit']);
Route::get ("/movie/index",[MovieController::class, 'index']);
Route::get ("/movie/show",[MovieController::class, 'show']);
Route::get ("/nationallity/create",[NationalityController::class, 'createShow']);
Route::post ("/nationallity/create",[NationalityController::class, 'store'])->name('nationallity.store');
Route::get ("/nationallity/edit",[NationalityController::class, 'edit'])->name('nationallity.edit');
Route::get ("/nationallity/index",[NationalityController::class, 'index']);
Route::get ("/nationallity/show",[NationalityController::class, 'show']);
Solution 1:[1]
You could do something like that:
Pass de actor id to the url:
<a href="/actor/show/{{ $actor['id] }}">.Define this route in
web.phpfile:
Route::get('/actor/show/{actor_id}', [ActorController::class, 'show'])
- In
ActorControllerget the actor from DB and send this data to a view, like you did before with the actor's list table.
Solution 2:[2]
usually when you do a basic crud, endpoint will be like this:
baseURL/actor/IDActor (Edit PUT method)
baseURL/actor/IDActor) (Delete DELETE method)
baseURL/actor/IDActor (See data GET method)
In web can be done like this:
baseURL/actor/edit/IDActor (edit actor)
baseURL/actor/delete/IDActor) (delete actor)
baseURL/actor/IDActor (get actor info)
just change the method:
POSTfor create,PUTfor modify,DELETEfor delete,GETfor get data
more info about methods here and the edit view call the Database and show the data.
If you make this your edit button was this:
<a href="/actor/edit/{{$actor['id']}}" class="btn btn-primary a-btn-slide-text">
<span class="glyphicon glyphicon-edit" aria-hidden="true"></span>
<span><strong>Edit</strong></span>
</a>
same for delete or view
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | entoniperez |
| Solution 2 | Voxxii |
