Sylar Sylar - 10 days ago 6
PHP Question

How to perfrom delete operation on a Model in Laravel 5?

Coming from a Ruby on Rails experience where you load up the rails console to delete a user or all users. I am new to Laravel 5 and I am looking for something similar to delete a user already in the sqlite3 database.

I see where people are talking about

User::find(1)->delete();
to delete a user but where to you put that and run in? Is there a console to perform a delete task in? I would like to know how to delete a user without dropping the table. I do not want to soft delete. Thanks.

Answer

You can put this code for example in controller.

You can use

$user = User::find($id);    
$user->delete();

if you don't use SoftDeletingTrait trait or

$user = User::find($id);    
$user->forceDelete();

if you do, and you want to really remove user from database, not just hide it from results.

More you can read at Laravel page