user3732216
user3732216

Reputation: 1589

Soft Deleting a row with laravel

I have a restful user controller with a listing of all users on my index method that is shown in an HTML table. Inside each row in a cell is e link to the edit page for a user and a link to soft delete a user. Right now I have it set up so that it will soft delete the user however I'm trying to work on it so that the link will actually work. What I used to have is just the anchor link however after watching Restful Forms video on the laracasts website I know this can't be done. Jeff Way presented the way of doing this action with Laravel as having to do so in a form.

Can someone suggest to me how to do so with me still being able to keep it looking the way I need it to with the classes assigned to the button and the i element as well?

Also when I click on what I have it goes to the following url which seems odd.

http://project.dev/users/{users}

I also have a dd($user_id) in the destroy method on the users controller and it keeps coming up NULL.

<td>
    <a href="{{ URL::route('users.edit', $user->id) }}" class="btn btn-xs btn-primary"><i class="fa fa-pencil"></i></a>
    {{ Form::open(['method' => delete', 'route' => 'users.destroy', $user->id]) }}
        {{ Form::button()}}
        <a href="{{ URL::route('users.destroy', $user->id) }}" class="btn btn-xs btn-danger"><i class="fa fa-times"></i></a>
    {{ Form::close() }}
</td>

Upvotes: 0

Views: 940

Answers (2)

Xris
Xris

Reputation: 76

Will something like this work?

<td>
    <a href="{{ URL::route('users.edit', $user->id) }}" class="btn btn-xs btn-primary"><i class="fa fa-pencil"></i></a>
    {{ Form::open(['method' => delete', 'route' => ['users.destroy', $user->id]]) }}
        {{ Form::button('<i class="fa fa-times"></i>', ['type' => 'submit', 'class' => 'btn btn-xs btn-danger'])}}
    {{ Form::close() }}
</td>

I adjusted the route parameter to include $user->id, and stuck the <i> into Form::button() ... Laravel doesn't escape the Form::button()'s value, so it goes through unmolested.

Upvotes: 1

Barry127
Barry127

Reputation: 1212

You could make an AJAX call using javascript. with jQuery it could be something like this:

(add ajax-delete class to the a element)

$('a.ajax-delete').on('click', function() {
    $this = $(this);
    $.ajax({
        type: 'DELETE',
        url: $this.attr('src'),
        success: function() {
            //dynammicaly remove deleted element from DOM tree here
        }
    });
});

Upvotes: 0

Related Questions