2017-01-01 4 views
0

私はコントローラでこのロジックを持っている:がどのように雄弁であることだろう - Laravel 5.2

// 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ではなく、コントローラの、モデルに存在することができることだと思う(私は持っていたいです'brief'コントローラ)。

いいですか?

もしそうなら、どうすればいいですか?

私のモデルは次のとおりです。あなたの提案を

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); 
} 

}

感謝。そして、すべての読者に幸せな新年。 ドム

答えて

0

Laravel 5. 3

あなたはMatiereモデルのメソッドを作成することによって、それを行うことができます。

Here is a simple example: 

ExampleController.php 

$mat = new \App\Matiere(); 

$feedback = $mat->addm($request); 



Matiere.php 

public function addm($request) 
{ 
    $this->id_ecole = \Session::get('id_ecole'); 
    $request->has('matiere_inactive') ? $this->matiere_inactive = '1' : $this->matiere_inactive = null; 

    $this->save(); 

    return $this->id; 
} 
+0

私はこの考えに感謝します。しかし、セッションをモデルに直接使用することをお勧めしますか? – Dom