0

I am using Laravel framework, where I have a form, which has a list of profiles displayed (using Profile Model). All these profiles are bound with a single checkbox array. There is a button at the top that when clicked, deletes the selected profiles (The action used here is ProfileController@deleteProfile). Also each profile has a button next to it, which when clicked, is supposed to go to ProfileController@editProfile action, but I am not sure where to specify this different action in the form. Is there a way to trigger a different action when Edit button is clicked?

@if(count($profiles) > 0) 

{!! Form::open(['action' => ['AdminController@deleteProfile'], 'method' => 'POST']) !!}
{{  Form::submit('Delete Selected Profiles', ['class' => 'btn btn-danger']) }}  
<table class="table">
    <thead class="thead-dark">
        <tr>
            <th scope="col">#</th>
            <th scope="col">Select</th>
            <th scope="col">Title</th>
            <th scope="col"></th>
        </tr>
    </thead>
    <tbody>
        @foreach($profiles as $profile)
            <tr>
                <th scope="row">{{ $profile->id }}</th>

                <td>   
                    <input type="checkbox" name="selectedProfiles[]" value="{{ $profile->id }}" />                           
                </td>
                <td>{{ $profile->title }}</td>
                <td>
                    {{  Form::hidden('id', $profile->id) }}    
                    {{  Form::submit('Edit', ['class' => 'btn btn-secondary']) }}                                  
                </td>
            </tr>
        @endforeach
    </tbody>
</table>
{!! Form::close() !!}

@endif

Another approach I tried was to create a generalized action ProfileModify where I would pass an extra variable to specify the kind of action I wanted to implement, 'Edit' or 'Delete' but I can't find a way to pass the Hidden input conditionally to send "Edit" or "Delete" when we click 2 separate buttons.

Can someone suggest how to approach this issue, and if this is actually possible, and if not, a better possible alternative to tackle this situation?

2
  • Does ProfileController@editProfile display the edit form for a profile? Commented Jun 13, 2019 at 7:24
  • Yes, it would display the edit form for the profile with the profile details mentioned. Commented Jun 13, 2019 at 7:30

1 Answer 1

1

You don't need a form submit to link to the edit page of the profile, just use an a tag.

If you have named routes:

{{ link_to_route('profile.edit', 'Edit', ['id' => $profile->id]) }}

For actions:

{{ link_to_action('ProfileController@editProfile', 'Edit', ['id' => $profile->id]) }}


Docs

Sign up to request clarification or add additional context in comments.

4 Comments

If the last parameter is the array of all parameters to be passed to the action editProfile, what does the 2nd parameter 'Edit' mean? I checked the Docs and there it says $title=null, which title is it referring to?
It is referring to the the text in the a tag: <a>Edit</a>
Also, it's passing the information to the action through the URL, is there a way to avoid it and pass it through POST without having to submit a form?
Why would you want to avoid passing it through the URL? It should be something like /profile/1/edit. If you want use POST then you need a form for it, but I don't see a reason why you would want that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.