Jay Gorio
Jay Gorio

Reputation: 335

Cannot insert values into database in laravel 5.1

I have this problem, when I return all values from the form input it all returns values that were filled in. However when I persist into the database under the store method in my controller it insert empty values. Can anyone help me on this? Thank you. Below are my codes.

public function store(EnrollmentRequest $request)
{   
    return $request->all();
    return redirect('/enrollment/create');
}

As you could see on top, it returns all inputs. However when I persist it on the database it returns only the id and timestamps;

public function store(EnrollmentRequest $request)
{   
    $enrollment = Enrollment::create([$request->all()]);
    return $enrollment;  return redirect('/enrollment/create');
}

Also here's my enrollment table:

    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();
    });

also my subjects table:

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();
    });

Your help are well appreciated.

   protected $table = 'enrollments';

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

Upvotes: 1

Views: 653

Answers (1)

James
James

Reputation: 16359

By the sounds of it you haven't updated the $fillable property of the Enrollment model.

As such you are running in to trouble with mass assignment and only the values that are fillable by default, ID and timestamps, are being inserted - the rest are simply ignored.

You need to add to your $fillable property for it to include the other fields that you want to have as fillable through mass assignment (i.e when you are running Enrollment::create([$request->all()]);).

It should look something like the below:

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

Upvotes: 1

Related Questions