Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent
Last updated 1 year ago.
0

Yeah, Laravel 4.2 now uses traits in softdeleting. Read here for more information.

Just make sure you have

use SoftDeletingTrait;

protected $dates = ['deleted_at'];

for every model you are implementing soft delete.

Upon deletion

User::find(1)->delete();

you should be able to retrieve it through this

User::withTrashed()->get()->toArray();

This is now easy to implement in your belongsTo relation

Last updated 1 year ago.
0

Right - I am using the new SoftDeletingTrait on the underlying model - 'Teacher' in this case - but it is unclear to me how I would restrict the results in the relation as the withTrashed() is not defined on BelongsTo or any of it's superclasses.

Last updated 1 year ago.
0

Yeah, its supposed to work, it works for me, just make sure you have the

use SoftDeletingTrait;
protected $dates = ['deleted_at'];

in whichever models you want to use the ->withTrashed()

Doing so will make it possible to chain the method

Look at what i have

*models/User.php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

	use UserTrait, RemindableTrait, SoftDeletingTrait;

	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'users';

	/**
	 * The attributes excluded from the model's JSON form.
	 *
	 * @var array
	 */
	protected $hidden = array('password', 'remember_token');
    /**
     * Blacklist
     *
     * Allow for mass Update
     *
     * @var string
     */
    protected $guarded = array();

    protected $dates = ['deleted_at'];

    public function userProfile()
    {
        return $this->hasOne('UserProfile')->withTrashed();
    }

}

*models/UserProfile.php

class UserProfile extends Eloquent {
    use SoftDeletingTrait;

    protected $table = 'user_profiles';
    /**
     * Blacklist
     *
     * Allow for mass Update
     *
     * @var string
     */
    protected $guarded = array();

    protected $dates = ['deleted_at'];

    public function user()
    {
        return $this->belongsTo('User')->withTrashed();
    }
}

And after softdeleting the user_profile, getting it from the User model like this, gives me back some data

Route::get('user', function()
{
    dd(User::find(1)->userProfile()->get()->toJSON());
});

Also if i softdelete the user, getting it from the UserProfile model like this, gives me some data back

Route::get('user', function()
{
    dd(UserProfile::find(1)->user()->get()->toJSON());
});
Last updated 1 year ago.
0

I've just updated to 4.2 and am having the exact same problem - evandertino - I've got a very similar set up to your example, but when adding:

public function user()
{
        return $this->belongsTo('User')->withTrashed();
}

I get:

Call to undefined method Illuminate\Database\Query\Builder::withTrashed()

Do you have any ideas for why this won't work?

Last updated 1 year ago.
0

@richbradshaw do you have this

use SoftDeletingTrait;

in both models you are trying to relate?

Last updated 1 year ago.
0

I had to add the use line in my model that extended my base model for it to work:

use Illuminate\Database\Eloquent\SoftDeletingTrait;

class Football extends Game {
    use SoftDeletingTrait;

}

Different file:

use Illuminate\Database\Eloquent\SoftDeletingTrait;

class Game extends Eloquent {
    use SoftDeletingTrait;

}
Last updated 1 year 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.