无法在laravel 5.1中插入值到数据库中
问题描述:
我有这个问题,当我从表单输入返回所有值时,它都返回填充的值。但是,当我在控制器中的存储方法下持久存储数据库时,它会插入空值。谁可以帮我这个事?谢谢。以下是我的代码。无法在laravel 5.1中插入值到数据库中
public function store(EnrollmentRequest $request)
{
return $request->all();
return redirect('/enrollment/create');
}
正如您在顶部看到的那样,它会返回所有输入。但是,当我坚持它在数据库上时,它只返回id和时间戳;
public function store(EnrollmentRequest $request)
{
$enrollment = Enrollment::create([$request->all()]);
return $enrollment; return redirect('/enrollment/create');
}
而且,这里是我的报名表:
Schema::create('enrollments', function (Blueprint $table) {
$table->increments('id');
$table->integer('student_id');
$table->string('subject_description');
$table->string('subject_code');
$table->time('schedule');
$table->string('room_no');
$table->integer('no_of_units');
$table->string('section');
$table->timestamps();
});
也是我的主题表:
Schema::create('subjects', function (Blueprint $table) {
$table->increments('id');
$table->string('subject_code');
$table->string('subject_description');
$table->time('schedule');
$table->string('room_no');
$table->integer('no_of_units');
$table->timestamps();
});
您的帮助是十分赞赏。
protected $table = 'enrollments';
protected $fillable = [
'subject_code',
'subject_description',
'section',
'schedule',
'room_no',
'no_of_units'
];
答
通过它的声音,你有没有更新的招生模式的$fillable
财产。
因此,您遇到了批量分配问题,并且只插入了默认可填充的值,ID和时间戳,其余部分仅被忽略。
您需要添加到您的$fillable
属性中,以包含您希望通过质量分配填写的其他字段(即,当您运行Enrollment::create([$request->all()]);
时)。
它应该看起来像下面:
protected $fillable = ['student_id', 'subject_description', 'subject_code', 'schedule', 'room_no', 'no_of_units', 'section'];
+0
也谢谢,但我已经解决了它,Minhajul的答案是正确的。 –
,你能否告诉我们该表的模式? – James
你可以试试$ enrollment = Enrollment :: create($ request-> all()); – Minhajul
在所有字段或任何特定字段上插入空值? –