简体   繁体   中英

Laravel - variable from form not being saved in create method

I am trying to save the information from a form, into the database. I can successfully save all fields minus one - a select field.

Form HTML (blade):

{!! Form::select('ClinicTypes', $clinicTypes, null, ['class' => 'form-control',
       'name' => 'speciality']) !!}

That displays in HTML:

<select class="form-control" name="speciality">
    <option value="1"></option>
    <option value="2"></option>
</select>

If I: dd($request->get('speciality')); it returns '1', which is correct, however, when I add this into the create function, it saves in the database as 0.

Snippet from create method:

'clinic_types_id' => $request->get('speciality'),

dd屏幕截图

Snippet from database:

数据库

Schema:

Schema::create('clinics', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('user_id');
            $table->integer('clinic_types_id');

Why is it saving as 0, instead of 1? Any help would be hugely appreciated. Many thanks.

You might make sure it's in the $fillable array for the model.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Clinic extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['clinic_types_id'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM