By what unique field (database column) do you identify users on both the users
and user_profiles
table?
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');
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();
}
$user = User::find($id);
should be
$user = User::find($user_id);
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community