У меня есть эта логика в контроллере :
// hydratation part 1 (cf son modele)
$matiere->fill($request->all());
// hydratation part 2 with attributes not in the POST
$matiere->id_ecole = Session::get('id_ecole');
$request->has('matiere_inactive') ? $matiere->matiere_inactive = '1' : $matiere->matiere_inactive = null;
if ($matiere->exists) {
$matiere->auteur_modif = Session::get('nom').' '.Session::get('prenom');
} else {
$matiere->auteur_creation = Session::get('nom').' '.Session::get('prenom');
}
Я думаю, что часть 2 может присутствовать в модели , а не в контроллере (мне нравится иметь «краткий» контроллер).
Это хорошая идея ?
Если да, то как я могу это сделать ?
Моя модель :
class Matiere extends Model {
protected $table = 'matiere';
protected $primaryKey = 'id_matiere';
protected $fillable = [
'id_matiere',
'code_court_matiere',
'libelle_matiere'
];
public function setCodeCourtMatiereAttribute($value)
{
$this->attributes['code_court_matiere'] = strtoupper($value);
}
public function setLibelleMatiereAttribute($value)
{
$this->attributes['libelle_matiere'] = ucwords($value);
}
}
Спасибо за ваши предложения. И с Новым годом всех читателей.
Дом
Laravel 5. 3
это можно сделать, создав метод на модели Matiere.