Edit for lack of coffee when originally responding and for much greater clarity.
- You should be able to do exactly as you ask (assuming here that the user's id is 1)
$user = App\User::find(1);
$user->delete();
If the foreign key's onDelete clause is defined, (which in your case it is), the user and ALL rows in other tables defined as you note in your example.
Your create_tasks_table foreign key should look like something like this:
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
Yours looks fine. Let's move on to testing some things. Use php artisan tinker and try this (assuming your user id is 1):
$user = App\User::find(1)->delete();
Tinker should output => true
Check your database. Is the user deleted? Are the user's tasks deleted?
- Try to run the query directly against the db and see what happens:
DELETE FROM users WHERE id = 1;
Was the user and the user's tasks deleted?
3a. From within Laravel, you can use the DB facade to wrap the delete function:
DB::delete('DELETE FROM users WHERE id = 1');
- You don't need to necessarily create foreign keys with the delete behavior. You can achieve deletion in the manner you are seeking by Relationship in your User model
public function tasks()
{
return $this->hasMany('App\Task');
}
And then,
$user = User::find($id);
$user->tasks()->delete();
$user->delete();
If you have foreign keys with cascading deletion, the point I was making is that the database will enforce those constraints. Some people would rather have more precise control over exactly what gets deleted. If you define foreign keys and casade deletion anywhere you reference, for example, a user's id, you have to remember that when a user is deleted all rows for that user in foreign tables will be deleted. Using Eloquent, you can be explicit about what gets deleted. I digress, that goes well beyond the scope of your question.
The tl;dr is that what you did should work? Without errors or code or other details all you can do is try a few other methods and see if you get the desired results.