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

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

Last updated 2 years ago.

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.

Last updated 2 years ago.

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
Last updated 2 years ago.
$user = User::find($id);

should be

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

Sign in to participate in this thread!


Your banner here too?


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

Your logo here?

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

© 2024 - All rights reserved.