在新添加数据的时候出现了这个问题,
stackoverflow上是这么解决的:http://stackoverflow.com/questions/34565515/laravel-5-massassignmentexception-in-model-php
然后我就去试了一试,还真的可以;
这个问题是我们没有在模型里面设置批量赋值。
Student.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
const SEX_UN = 10;// 未知
const SEX_BOY = 20;// 男士
const SEX_GRIL = 30;// 女生
protected $table = 'student';
protected $fillable = ['name', 'age' ,'sex'];//添加这句,数组的参数跟你数据库里面表的字段一致
SutudentController.php
控制器里的的话
public function create(Request $request){
$data = $request ->input('Student');
// 将新增加的数据传进create方法里面
if (Student:: create($data)) {
return redirect('student/index') ->with('success' ,'添加成功!');
} else {
return redirect()->back();
}
}