stefan de boer
stefan de boer

Reputation: 405

How to delete data from database in laravel?

In my website, I have a page where users can see their orders. In this table, I want to be able to delete the order by clicking a link. However, everything i tried so far isn't working. I have no idea what I'm doing wrong or what I can do to make I work. It keeps giving me a 404 error.

My Route:

Route::get('destroy/{$id}', 'AccountController@destroy');

My Controller function:

public function destroy($id)
{
    RestaurantModel::where('id', $id)->delete();

    return back('/');
}

My Model: Don't mind the model name.

class RestaurantModel extends Model
{
protected $table = 'orders';
protected $primarykey = 'orderNumber';
} 

The row from the table in my view: The delete function is at the back of the code.

@foreach ($orders as $order)
            <tr>
                 <td><a href="">{{{ $order->orderNumber }}}</a></td>
                 <td>{{{ $order->orderDate }}}</td><td>{{{ $order->shippedDate }}}</td>
                 <td>{{{ $order->status }}}</td>
                 <td>{{{ $order->comments }}}</td>
                 <td>{{{ $order->customerNumber }}}</td>
                 <td><a href="{{ action('AccountController@destroy', $order->orderNumber) }}">Delete order</a></td>
            </tr>
@endforeach

I hope someone can help me with my problem. I've been stuck on this for a while now.

Upvotes: 1

Views: 964

Answers (1)

Stormhammer
Stormhammer

Reputation: 497

Try changing your route

Route::get('destroy/{$id}', 'AccountController@destroy');

to

Route::get('destroy/{id}', 'AccountController@destroy');

Upvotes: 1

Related Questions