2016-01-24 41 views
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' 
]; 
+0

,你能否告訴我們該表的模式? – James

+1

你可以試試$ enrollment = Enrollment :: create($ request-> all()); – Minhajul

+0

在所有字段或任何特定字段上插入空值? –

回答

1

通過它的聲音,你有沒有更新的招生模式的$fillable財產。

因此,您遇到了批量分配問題,並且只插入了默認可填充的值,ID和時間戳,其餘部分僅被忽略。

您需要添加到您的$fillable屬性中,以包含您希望通過質量分配填寫的其他字段(即,當您運行Enrollment::create([$request->all()]);時)。

它應該看起來像下面:

protected $fillable = ['student_id', 'subject_description', 'subject_code', 'schedule', 'room_no', 'no_of_units', 'section']; 
+0

也謝謝,但我已經解決了它,Minhajul的答案是正確的。 –

相關問題