SQLSTATE[HY000]: General error: 1364 Field 'branch_id' doesn't have a default value laravel 5.4
SQLSTATE[HY000]: General error: 1364 Field 'branch_id' doesn't have a default value laravel 5.4
大家好,我需要你们的帮助。我是 laravel 5.4 的初学者。我正在尝试添加一个外键字段,但它没有添加并产生错误没有默认值。
Add view
Then the error
用户控制器代码:
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');
}
用户模型:
public function branch()
{
return $this->belongsTo(Branch::class);
}
用户迁移:
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 用户
<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>
您需要将 index()
和 nullable()
添加到您声明 branch_id
的迁移文件中,如下所示:-
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');
});
}
编辑:- 在您的表单中更改此项。您需要在 foreach
之前声明选项值,如下所示:-
<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>
如果还没有解决,请检查 branch_id
是否添加到 fillable
in User
模型中。没加就加
您应该像这样在用户模型上添加 branch_id
作为可填充的 :
protected $fillable = [
'username', 'email', 'password', 'branch_id'
];
希望对你有所帮助
branch_id
对模型不可见,因为它不在可填充数组中。
在App\User.php
protected $fillable = [ ... ]; // add branch_id
// or
protected $guarded = [];
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 (*)
}
在此代码中,您 2 次在数据库中进行请求。而不是这个使用
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());
将打印
array:100 [▼
1 => "branch1"
2 => "branch2"
3 => "branch3"
4 => "branch4"
.
.
.
]
在视图中修复它
@foreach ($branches as $branchId => $branchName)
<option value= "{{ $branchId}}">
{{ $branchName }}
</option>
@endforeach
和User
模型在fillable
属性添加branch_id
大家好,我需要你们的帮助。我是 laravel 5.4 的初学者。我正在尝试添加一个外键字段,但它没有添加并产生错误没有默认值。
Add view
Then the error
用户控制器代码:
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');
}
用户模型:
public function branch()
{
return $this->belongsTo(Branch::class);
}
用户迁移:
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 用户
<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>
您需要将 index()
和 nullable()
添加到您声明 branch_id
的迁移文件中,如下所示:-
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');
});
}
编辑:- 在您的表单中更改此项。您需要在 foreach
之前声明选项值,如下所示:-
<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>
如果还没有解决,请检查 branch_id
是否添加到 fillable
in User
模型中。没加就加
您应该像这样在用户模型上添加 branch_id
作为可填充的 :
protected $fillable = [
'username', 'email', 'password', 'branch_id'
];
希望对你有所帮助
branch_id
对模型不可见,因为它不在可填充数组中。
在App\User.php
protected $fillable = [ ... ]; // add branch_id
// or
protected $guarded = [];
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 (*)
}
在此代码中,您 2 次在数据库中进行请求。而不是这个使用
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());
将打印
array:100 [▼
1 => "branch1"
2 => "branch2"
3 => "branch3"
4 => "branch4"
.
.
.
]
在视图中修复它
@foreach ($branches as $branchId => $branchName)
<option value= "{{ $branchId}}">
{{ $branchName }}
</option>
@endforeach
和User
模型在fillable
属性添加branch_id