大家好,我需要你的帮助。我是laravel 5.4的初学者。我试图添加一个外键字段,但它没有添加并产生错误没有默认值。
UsersController代码:
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>
答案 0 :(得分:3)
branch_id
对模型不可见,因为它不在可填充数组中。
在App \ User.php中
protected $fillable = [ ... ]; // add branch_id
// or
protected $guarded = [];
答案 1 :(得分:0)
您需要将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
是否添加了User
。如果没有添加,请添加。
答案 2 :(得分:0)
您应该在用户模型上添加branch_id
,如下所示:
protected $fillable = [
'username', 'email', 'password', 'branch_id'
];
我希望这可以帮到你
答案 3 :(得分:0)
const someArray = someJsonObj.map( obj => Object.values(obj) );
在这段代码中你有两次在db中请求。而不是这个使用
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 (*)
}
将打印
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