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

Hi,

try

$user->posts()->detach( $post)
Last updated 2 years ago.
0

l3hash said:

Hi,

try

$user->posts()->detach( $post)

I don't think this will work because its not a many-to-many relationship.

Have you tried just setting the foreign id to null manually?

$user->post_id = null;
$user->save();
Last updated 2 years ago.
0

I think it is destroy

$user->posts->destroy()

There is also soft deleting, you add a column called deleted_at then it marks records as deleted but keeps them for historical reasons

http://laravel.com/docs/eloquent#soft-deleting

Last updated 2 years ago.
0

l3hash said:

Have you tried just setting the foreign id to null manually?

$user->post_id = null; $user->save();

I have. My concern is the following.

$user->posts->save($post);
$user->posts->count(); // returns 1
$post->user_id = null;
$post->save();
$user->posts->count(); // still returns 1, instead of 0!

Any idea? I'm thinking the lack of actual method for doing so leads to model events not being triggering, etc. Yet I don't want to dive into Laravel's internal.

As a fall back I might do what IanSirkit suggested, using destroy() and then dealing with the removed element as a batch using the withTrashed() method. But it's not exactly what I wanted to do in the first place...

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

dheavy dheavy Joined 10 May 2014

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.