SQLSTATE[HY000]: General error: 1364 Field 'branch_id' doesn't have a default value laravel 5.4

怎甘沉沦 提交于 2020-12-14 11:07:34

问题


Hello guys I need your help. I am a beginner in laravel 5.4. I am trying to add a field which is foreign key however it doesn't added and produce the error doesn't have a default value.

Add view

Then the error

UsersController code:

    public function create()
{
    $branches = Branch::pluck('name', 'id');
    return view('users.create', ['branches' => Branch::all()]);
}


public function store(Request $request)
{
    $this->validate($request, [
        'branch_id' => 'required',
        'fname' => 'required',       
        'lname' => 'required',
        'contact_number' => 'required',
        'bday' => 'required',
        'position' => 'required',
        'status' => 'required',
        'username' => 'required',
        'password' => 'required'
    ]);

    User::create($request->all());

    session()->flash('success_msg', 'Employee has been added!');
    return redirect('/employees');
}

User Model:

public function branch()
{
  return $this->belongsTo(Branch::class);
}

User Migration:

public function up()
{
    Schema::dropIfExists('users');
    Schema::disableForeignKeyConstraints();
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('branch_id')->unsigned();
        $table->string('fname');
        $table->string('lname');
        $table->string('contact_number');
        $table->date('bday');
        $table->integer('position');
        $table->integer('status');
        $table->string('username');
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();

        $table->foreign('branch_id')->references('id')->on('branches')->onDelete('cascade');

    });

}

create.blade.php in user

<div class="form-group">
    <label for="branch_id">Branch: </label>
    <!--{{ Form::select('branch_id', $branches, null) }}-->
    <select class="form-control" name="branch_id">
        @foreach ($branches as $branch)
            <option value= "{{ $branch->id}}">
              {{ $branch->name }}
            </option>
        @endforeach
    </select>
  </div>

回答1:


The branch_id is not visible to the model because it's not in fillable array.

In App\User.php

protected $fillable = [ ... ]; // add branch_id
// or
protected $guarded = [];



回答2:


You should add branch_id on User model as fillable like this :

 protected $fillable = [
    'username', 'email', 'password', 'branch_id'
];

I hope this help you




回答3:


you need to add index() and nullable() to your migration file where you have declared the branch_id as follows:-

public function up()
{
Schema::dropIfExists('users');
Schema::disableForeignKeyConstraints();
Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('branch_id')->unsigned()->index()->nullable();
    $table->string('fname');
    $table->string('lname');
    $table->string('contact_number');
    $table->date('bday');
    $table->integer('position');
    $table->integer('status');
    $table->string('username');
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();

    $table->foreign('branch_id')->references('id')->on('branches')->onDelete('cascade');

});

}

Edit:- change this in your form. you need to declare the option value before foreach as follows:-

<div class="form-group">
<label for="branch_id">Branch: </label>
<!--{{ Form::select('branch_id', $branches, null) }}-->
<select class="form-control" name="branch_id">
<option value="" disabled selected>{{ trans('select_branch') }}</option>   
    @foreach ($branches as $branch)
        <option value= "{{ $branch->id}}">
          {{ $branch->name }}
        </option>
    @endforeach
</select>

If not yet resolved check whether branch_id is added in the fillable in User model or not. Add it if it isn't added.




回答4:


public function create()
{
    $branches = Branch::pluck('name', 'id');  // first time request
    return view('users.create', ['branches' => Branch::all()]); 
    // Branch::all() second time request and select all (*)
}

In this code you 2 time do request in db. Instead of this use

public function create()
{
    $branches = Branch::pluck('name', 'id');
    return view('users.create', compact('branches'));
    // or return view('users.create', ['branches' => ]);
}


 $branches = Branch::pluck('name', 'id');
 dd($branches->all());

will print

array:100 [▼
  1 => "branch1"
  2 => "branch2"
  3 => "branch3"
  4 => "branch4"
  .
  .  
  .
]

In view fix it

@foreach ($branches as $branchId => $branchName)
   <option value= "{{ $branchId}}">
      {{ $branchName }}
   </option>
@endforeach

And the User model in fillable property add branch_id



来源:https://stackoverflow.com/questions/50791331/sqlstatehy000-general-error-1364-field-branch-id-doesnt-have-a-default-va

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!