Home > Back-end >  How do I keep the selected entry in the table and use it on the next view?
How do I keep the selected entry in the table and use it on the next view?

Time:03-31

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  href="/actor/create" role="button">Actors</a>

<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" >
                    <span  aria-hidden="true"></span>
                    <span><strong>Edit</strong></span>            
                </a>
                <a href="/actor/show" >
                    <span  aria-hidden="true"></span>
                    <span><strong>View</strong></span>            
                </a>
                    <a href="#" >
                        <span  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?

CodePudding user response:

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:

POST for create, PUT for modify, DELETE for delete, GET for 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['actorName']}}" >
    <span  aria-hidden="true"></span>
    <span><strong>Edit</strong></span>
</a>

same for delete or view

CodePudding user response:

You could do something like that:

  1. Pass de actor id to the url: <a href="/actor/show/{{ $actor['id] }}">.

  2. Define this route in web.php file:

Route::get('/actor/show/{actor_id}', [ActorController::class, 'show'])

  1. In ActorController get the actor from DB and send this data to a view, like you did before with the actor's list table.
  • Related