Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

By what unique field (database column) do you identify users on both the users and user_profiles table?

Last updated 2 years ago.
0

You don't need to implement your own methods to do that. It is usual that when two tables are linked with a foreign key that after deleting a record from one table, some records in other tables don't make sense. Most databases have a "ON DELETE" option when defining a foreign key. There also exists a "ON UPDATE" behaviour.
To delete also the dependent rows in other tables, define the ON DELETE CASCADE.
You do that in your migrations. If your database already contains data, you can change the onDelete behaviour in phpMyAdmin.

$table->foreign('FK_column')
->references('other_table')->on('id')
->onDelete('cascade')
->onUpdate('cascade');
Last updated 2 years ago.
0
Solution

In code it would look something like this:

public function destroy(id) // for when you append the id in the URL
{
    // Somehow retrieve/define the user's identifying field (here: id)
    $user_id = Input::get('id');
    // or
    $user_id = $id; // when you append the id in the URL

    $user = User::find($id);
    
    $user->profile->delete(); // For this to work you need to define the profile relationship in the user model
    $user->delete();
}
Last updated 2 years ago.
0
$user = User::find($id);

should be

$user = User::find($user_id);
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.