If you're deleting the company record, then you can't add a value to any of the records attributes unless your using soft deletes: http://laravel.com/docs/eloquent#soft-deleting
Thanks ddimaria,
But on my question i'm already setup my model to use soft delete, in my model i already set property $softDelete = true;
The model doesn't get saved when you delete a record. I looked at the Lararel code, and if you're soft deleting it only updates the one column.
if ($this->softDelete)
{
$this->{static::DELETED_AT} = $time = $this->freshTimestamp();
$query->update(array(static::DELETED_AT => $this->fromDateTime($time)));
}
So you'd probably want to handle the deleted event instead, and then save the model:
$company->deleted_by = Auth::user()->id;
$company->save();
Not tested it, but might point you in the right direction.
Ok so I tried those solutions but they don't work, So the only way to do so was by saving the $model on the deleting event, I have a BaseModel class that handles all the deletes, updates and creation events that all my Models inherit from. So in my base model and in the boot() method I put:
static::deleting(function($model){
$model->deleted_by = Auth::getUser();
$model->save();
})
and now all my models are updated during deletion, Hope that helps.
Hi guys, I have the same problem.
This is my BaseModel.php
<?php
class BaseModel extends Eloquent {
/**
* Events
*
* @return Void
*/
public static function boot()
{
parent::boot();
// Setup event bindings...
//----------------------------------------------------
// DELETING EVENTS
//----------------------------------------------------
static::deleting(function($model){
$aUserSession = Session::get('user_session');
if($aUserSession['id']!='')
{
$model->deleted_by = $aUserSession['id'];
dd($model);
exit();
$model->save();
return true;
}
else
{
return false;
}
});
//----------------------------------------------------
// DELETING EVENTS
//----------------------------------------------------
static::creating(function($model){
$aUserSession = Session::get('user_session');
if($aUserSession['id']!='')
{
$model->created_by = $aUserSession['id'];
$model->save();
return true;
}
else
{
return false;
}
});
//----------------------------------------------------
// DELETING EVENTS
//----------------------------------------------------
static::updating(function($model){
$aUserSession = Session::get('user_session');
if($aUserSession['id']!='')
{
$model->updated_by = $aUserSession['id'];
$model->save();
return true;
}
else
{
return false;
}
});
}
}
And this is my UserModel.php
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
use Illuminate\Database\Eloquent\SoftDeletingTrait;
class User extends BaseModel implements UserInterface, RemindableInterface {
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
/**
* 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');
/**
* Generate Hash password to user on save
*
* @return Hash
*/
public function setPasswordAttribute($password){
$this->attributes['password'] = Hash::make($password);
}
/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the token value for the "remember me" session.
*
* @return string
*/
public function getRememberToken()
{
return $this->remember_token;
}
/**
* Set the token value for the "remember me" session.
*
* @param string $value
* @return void
*/
public function setRememberToken($value)
{
$this->remember_token = $value;
}
/**
* Get the column name for the "remember me" token.
*
* @return string
*/
public function getRememberTokenName()
{
return 'remember_token';
}
/**
* Get the e-mail address where password reminders are sent.
*
* @return string
*/
public function getReminderEmail()
{
return $this->email;
}
}
When I try to delete, create or update an error ocurr.
I checked the values of $model->deleted_by, $model->created_by an $model->updated_by and change correctrly, but when method save it'is called nothing happends.
Any idea?
Kind regards
I use Laravel's soft-delete for some of my models. However, in addition to the deleted_at column, I want a delete_by one too.
I use the following code to implement 'deleted_by' functionality. Additionally, this code does not change the updated_at column (by design)
I have created my own partial copy of SoftDeletingTrait that replaces runSoftDelete().
trait MySoftDeletingTrait {
/**
* Perform the soft-delete, by setting the deleted_at and deleted_by columns.
* Do not use model->update to do this, as it would also update the updated_at column!
*/
protected function runSoftDelete()
{
$keyName = $this->getKeyName();
$keyValue = $this->getKey();
$tableName = $this->getTable();
if (key_exists('deleted_by', $this->attributes)) {
$updateColumns = [$this->getDeletedAtColumn() => $this->freshTimestamp(), 'deleted_by' => Auth::user()->id];
} else {
$updateColumns = [$this->getDeletedAtColumn() => $this->freshTimestamp()];
}
// Do not use the model to do the update, as it will also update the updated_at column.
DB::table($tableName)
->where($keyName, '=', $keyValue)
->update($updateColumns);
}
}
My model has this in it
class Wholesaler extends MyModelBase {
use MySoftDeletingTrait, Illuminate\Database\Eloquent\SoftDeletingTrait {
MySoftDeletingTrait::runSoftDelete insteadof Illuminate\Database\Eloquent\SoftDeletingTrait;
}
// etc etc
the 'use' line specifies that php should use the method runSoftDelete() from my trait. This is needed, as php would otherwise throw an error about methods with the same name.
I hope this helps
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community