Я новичок в использовании черт, и у меня возникли проблемы с сохранением защищенных атрибутов моей черты в моей красноречивой модели:
Вот моя модель маршрута:
namespace App\Models;
use Eloquent;
class Route extends Eloquent {
use CardTrait {
CardTrait::__construct as __CardConstruct;
}
public $timestamps = false;
protected $table = 'routes';
protected $primaryKey = 'id';
protected $visible = [
'name',
'description'
];
public function __construct(array $attributes = array())
{
$this->__CardConstruct($attributes);
}
//relationships follow
}
а вот черта CardTrait:
namespace App\Models;
trait CardTrait {
protected $timesAsked;
protected $factor;
protected $nextTime;
public function __construct($attributes = array(), $timesAsked = 0, $factor = 2.5, $nextTime = null)
{
parent::__construct($attributes);
if (is_null($nextTime)) $nextTime = \Carbon::now()->toDateTimeString();
$this->factor = $factor;
$this->nextTime = $nextTime;
$this->timesAsked = $timesAsked;
public function answer($difficulty)
{
if($difficulty < 3)
$this->timesAsked = 1;
}
//other methods follow
}
В моем контроллере я могу использовать:
$route = new Route();
$route->name = "New Name";
$route->description = "New Description";
$route->answer(5);
$route->save();
name
и description
сохраняются нормально, и хотя у меня есть столбцы для timesAsked
, factor
и nextTime
, когда я dd($route) я вижу
protected 'timesAsked' => int 1
protected 'factor' => float 2.6
protected 'nextTime' => string '2015-04-15 21:36:53' (length=19)
поэтому я знаю, что методы Черты работают нормально.
Мой вопрос: как я могу сохранить эти значения с помощью Eloquent, чтобы их можно было хранить и извлекать из базы данных?
Заранее спасибо.