laravel中fillable 和guarded该怎么用_问答专区_三更技术社区

我这么用的

Model里:

class Question extends Model {

//定义可以批量赋值的黑名单,定义白名单是protected $fillable
protected $guarded = \['id', 'created_at', 'updated_at'\];

public function belongsToDiffiuclty()
{

return $this->belongsTo('Difficulty', 'difficulty_id', 'id');

}

public function scopeDifficultyEasy($query) {

  return $query->where('difficulty_id', 1);

}

public function scopeDifficultyNormal($query) {

  return $query->where('difficulty_id', 2);

}

public function scopeDifficultyHard($query) {

  return $query->where('difficulty_id', 3);

}}

控制器里的方法:

public function store(Request $request)

{
    $this->validate($request,\[//          'title'=>'required|unique:questions|max:255',
        'title'=>'required|max:255',
        'body'=>'required',
    \]);
    $question = new Question;
    $question->title          = Input::get('title');
    $question->body           = Input::get('body');
    $question->difficulty_id  = Input::get('difficulty');
    $question->major_id       = Input::get('major');
    $question->user_id        = 1;//Auth::user()->id;
    if($question->save()){
        return Redirect::to('questions');
    }else{
        return Redirect::back()->withInput()->withErrors('保存失败!');
    }
}

Original url: Access
Created at: 2018-11-15 15:34:55
Category: default
Tags: none

请先后发表评论
  • 最新评论
  • 总共0条评论