BigJobbies
BigJobbies

Reputation: 193

Laravel 4 - Deleting a record from the database

Once again, i am stumped.

I have a table of letters, each with the following link attached

<a href="">Delete {{ $letter->lettername }}</a>

From the docs i can see i have to run the following from my routes.php

$letter = Letter::find(1);
$letter->delete();

My question is, what do i have to enter in the href to pass the letters ID onto the routes file, and then how do i pass that to the find() parameter?

Do i do something like this

<a href="letters/delete/IDHERE">Delete {{ $letter->lettername }}</a>

If so, how do i put that ID into the find paramter.

Im confused.

Any help would be greatly appreciated

Cheers

My Routes file is as follows:

Route::get('letters', array(
'before' => 'auth|userdetail',
function()
{

    // Grab the letters, if any, for this user
    $letters = Letters::forUser(Auth::user())->get();

    $data = [
        'letters' => $letters
    ];

    return View::make('letters', $data);

}
));

Upvotes: 1

Views: 472

Answers (1)

Laurence
Laurence

Reputation: 60048

You could do it like this

<a href="letters/delete/{{ $letter->id }}">Delete {{ $letter->lettername }}</a>

But it would be better if you have a named route

<a href="{{ URL::route('letters.delete', $letter->id) }}">Delete {{ $letter->lettername }}</a>

Then in your routes

Route::get('letters/delete/{id}', array('as' => 'letters.delete', 'before' => 'auth|userdetail', function($id)
{
    echo ("You want to delete letter id: " . $id . " from the system");
    // Put your delete code here
}

Upvotes: 4

Related Questions