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_by
とupdated_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
希望します。
残念ながら、 '$ fillable'プロパティは 'use CrudTrait;'と同様に存在しません。私は以前に忘れてしまったこと:/なぜ私はそのパラメータを更新したり保存したりすることができないのか分からない。 –
Hmm ... FormRequestではなく、AuthorCrudController :: update()メソッドで追加してみてください。 – tabacitu
またはこれにバックパックを使用して、その値をデフォルトとして隠しフィールドを追加することができます。 – tabacitu