我必须在编辑文章时传递用户的秘密 author_id ,然后将其记忆到Backpack-Laravel中的数据库中。 怎么办?
我能够做到这一点,该值出现在 $ request 数组中(我使用dd($ request)知道)但是没有存储在数据库中。
AuthorCrudController.php
public function update(UpdateArticleRequest $request)
{
//dd($request); <-- author_id = Auth::id()
return parent::updateCrud();
}
UpdateArticleRequest.php
public function rules()
{
$this->request->add(['author_id'=> Auth::id()]);
return [
'title' => 'required|min:5|max:255',
'author_id' => 'numeric'
];
}
答案 0 :(得分:3)
100次中的99次,当未存储该值时,因为该模型的Session.getEffectiveUser().getEmail();
属性中未提及该列。这是吗?
旁注:像这样添加author_id有效,但如果您将此方法用于多个模型,我建议您为所有模型编写一次。我为此使用了一个特征。这样,无论何时创建条目,作者都会被保存,并且您拥有将其放在一个位置的所有方法,即特征($fillable
,$this->creator()
)。
我的方法是:
1)我的数据库this->updator
和created_by
2)我使用这样的特征:
updated_by
3)每当我想要一个模型来拥有这个功能时,我只需要:
<?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');
}
}
希望它有所帮助。
答案 1 :(得分:0)
我的背包设置中的更新功能在updateCrud函数中传递了$ request。您提到的那个没有将请求传递给父函数。
public function update(UpdateRequest $request)
{
// your additional operations before save here
$redirect_location = parent::updateCrud($request);
return $redirect_location;
}