Reputation: 11
The problem I'm having is that I'm trying to make it so when I delete a users account, all the things accociated with the account get deleted to. The problem here is that, in my table where the users information is saved, has no foreign key references, they all come from other tables towards my users table.
Here is an example (links, because I'm not high enough to post images):
My problem (pink is what i have, blue is what i want), made simple (in paint):
Here is my view code for the delete button:
<th> <a href="/instagramDelete/{{$instagramAccount->id}}"> <button class="btn btn-danger btn-circle btn-sm"><i class="fas fa-trash"></i></button> </a> </th>
Code from my controller:
public function delete($id)
{
DB::table('instagram_accounts')->where('id', $id)->delete();
return redirect('/instagram');
}
(only works if other pages accociated with the user have no data from said user)
Code from my web (I have no idea if I need to include this here):
Route::get('/instagramDelete/{id}', 'InstagramAccountController@delete')->middleware('auth');
Code from my model:
class InstagramAccount extends Model
{
public function configuration()
{
return $this->hasOne(Configuration::class, 'account_id');
}
}
I hope this is enough for you to know what I mean, because i think this question is kinda vague.
If you need any more code or something, please ask.
As always, thanks in advance!
Upvotes: 1
Views: 66
Reputation: 111
you can use cascade in your migration file to achieve this. like this, when you delete from parent table, related data on other table will be deleted
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
reff: https://laravel.com/docs/5.8/migrations#foreign-key-constraints
Upvotes: 2