I'm just thinking aloud but wouldn't you just set the Profile to deleted when you soft_delete the User?
Soft Deleting A User And Their Profile
I am trying to figure out what I should do in this situation. I am wanting to soft delete a user and then soft delete their profile that way it can keep the data in my db in case the user decides to come back later and have an account again. What is the best route for me to go to not only soft delete the user but soft delete their profile as well.
As you can see I have applied the SoftDeletes Trait to my User model and have my relationship set up correctly between my user and their profile. How can I pass the soft delete down to the profile?
<?php namespace App;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use App\Presenters\PresentableTrait;
use App\Presenters\Contracts\PresentableInterface;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract, PresentableInterface {
use Authenticatable, CanResetPassword, PresentableTrait, SoftDeletes;
/**
* The database table that is to be used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* Add necessary date fields are to be used as Carbon instances.
*
* @var array
*/
protected $dates = ['deleted_at'];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['first_name', 'middle_name', 'last_name', 'username', 'email_address', 'password', 'confirmation_code', 'confirmed'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
/**
* Get the presenter that is to be used by the model.
*
* @var array
*/
protected $presenter = 'App\Presenters\User';
/**
* Set a token for creating a new user.
*/
public static function boot()
{
parent::boot();
static::creating(function($user) {
$user->confirmation_code = str_random(30);
});
}
/**
* Encrypt all passwords before creating a user.
*
* @param $password
*/
public function setPasswordAttribute($password)
{
$this->attributes['password'] = bcrypt($password);
}
/**
* Get the role that is associated with a given user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function role()
{
return $this->hasOne('App\Role', 'role_id', 'id');
}
/**
* Get the user profile that is associated with a given user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function profile()
{
return $this->hasOne('App\UserProfile', 'user_id', 'id');
}
/**
* Get the activity that is associated with a given user.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function activity()
{
return $this->hasMany('App\Activity');
}
}
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Repositories\Users\UserRepository;
use Illuminate\Http\Request;
class UsersController extends Controller {
/**
* @var UserRepository
*/
private $userRepository;
/**
* @param UserRepository $userRepository
*/
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
/**
* Remove the specified user.
*
* TODO: Find out what to do when deleting a user and needing to delete the user profile.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
$this->userRepository->delete($id);
return redirect('users');
}
}
@xtremer360 Why would you do this:
$profile = Profile::find($someId);
instead of this?
$user = User::find($someId);
$user->profile->something
Red this
That said, in fact it doesn't matter if profile is soft deleted or not.
Anyway, you can chain soft deleting during deleted event (ofc profile must use soft deletes as well).
Please or to participate in this conversation.