2016-10-12 9 views
0

記事の編集時にユーザーのauthor_idの秘密を渡し、Backpack-Laravelのデータベースに暗記する必要があります。 どうすればいいですか?Backpack-Laravelへのリクエストアイテムの追加方法は?

この値は、配列(私はそれを知るためにdd($ request)を使用します)に配列で表示されますが、データベースには格納されません。その列が言及されていないので、値は、それがだ格納されていない100のうち

AuthorCrudController.php

public function update(UpdateArticleRequest $request) 
{ 
    //dd($request); <-- author_id = Auth::id() 
    return parent::updateCrud(); 
} 

UpdateArticleRequest.php

答えて

3
public function rules() 
{ 

    $this->request->add(['author_id'=> Auth::id()]); 
    return [ 
     'title' => 'required|min:5|max:255', 
     'author_id' => 'numeric' 
    ]; 
} 

99回モデルの$fillableプロパティでこれでしょうか?


追記:このようなAUTHOR_IDを追加すると動作しますが、あなたは複数のモデルのために、このアプローチを使用している場合、私はすべてのあなたのモデルに一度コーディングをお勧めします。私はこれのために特性を使用します。その方法は、エントリが作成されるたびに、作成者は保存され、1つの場所、特性($this->creator()this->updator)で取得するためのすべてのメソッドがあります。

これへの私のアプローチはこれです:

1)私は私のデータベースに2つの新しい列created_byupdated_by

2を持っている)私はこのような形質を使用します。

<?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)モデルにこの機能を追加したい場合は、次のようにする必要があります。

<?php 

namespace App\Models; 

use Illuminate\Database\Eloquent\Model; 
use Backpack\CRUD\CrudTrait; 

class Car extends Model 
{ 
    use CrudTrait; 
    use CreatedByTrait; // <---- add this line 

希望します。

+0

残念ながら、 '$ fillable'プロパティは 'use CrudTrait;'と同様に存在しません。私は以前に忘れてしまったこと:/なぜ私はそのパラメータを更新したり保存したりすることができないのか分からない。 –

+0

Hmm ... FormRequestではなく、AuthorCrudController :: update()メソッドで追加してみてください。 – tabacitu

+0

またはこれにバックパックを使用して、その値をデフォルトとして隠しフィールドを追加することができます。 – tabacitu

関連する問題