99 times out of 100, when the value isn't stored it's because that column hasn't been mentioned in your model's $fillable
property. Is this it?
Sidenote: Adding the author_id like this works, but if you're using this approach for multiple models, I recommend coding it once for all your models. I use a trait for this. That way, any time an entry is created, the author is saved and you have the have all the methods for getting it in one place, the trait ($this->creator()
, this->updator
).
My approach to this is this:
1) I have two new columns in my database created_by
and updated_by
2) I use a trait like this:
<?php namespace App\Models\Traits;
use Illuminate\Database\Eloquent\Model;
trait CreatedByTrait {
/**
* Stores the user id at each create & update.
*/
public function save(array $options = [])
{
if (\Auth::check())
{
if (!isset($this->created_by) || $this->created_by=='') {
$this->created_by = \Auth::user()->id;
}
$this->updated_by = \Auth::user()->id;
}
parent::save();
}
/*
|--------------------------------------------------------------------------
| RELATIONS
|--------------------------------------------------------------------------
*/
public function creator()
{
return $this->belongsTo('App\User', 'created_by');
}
public function updator()
{
return $this->belongsTo('App\User', 'updated_by');
}
}
3) Whenever I want a model to have this feature, i just need to:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;
class Car extends Model
{
use CrudTrait;
use CreatedByTrait; // <---- add this line
Hope it helps.