我想将department
表的主键存储到section
表中。
迁移
Schema::create('tbl_department', function (Blueprint $table) {
$table->string('id')->primary();
$table->string('department_name')->nullable();
$table->softDeletes();
$table->timestamps();
});
Schema::create('tbl_section', function (Blueprint $table) {
$table->string('id')->primary();
$table->string('department_id');
$table->foreign('department_id')
->references('id')
->on('tbl_department')
->onDelete('cascade')
->onUpdate('cascade');
$table->string('section_name')->nullable();
$table->softDeletes();
$table->timestamps();
});
部门模型
class DepartmentModel extends Model
{
protected $table = 'tbl_department';
protected $fillable = ['department_name'];
public function section () {
return $this->hasMany('App\SectionModel', 'department_id');
}
部分模型
class SectionModel extends Model
{
protected $table = 'tbl_section';
protected $fillable = ['department_id', 'section_name'];
public function department () {
return $this->belongsTo('App\DepartmentModel', 'department_id');
}
控制器
public function store(Request $request)
{
$request->validate ([
'department' => 'required',
'section' => 'required'
]);
DepartmentModel::create ([
'department_name' => $request->department,
]);
SectionModel::create ([
'section_name' => $request->section,
]);
return redirect()->route('department.index');
}
答案 0 :(得分:0)
编辑此内容...
迁移
Schema::create('tbl_department', function (Blueprint $table) {
$table->bigIncrements('id');
...
});
Schema::create('tbl_section', function (Blueprint $table) {
$table->bigIncrements('id');
...
});
部分模型
public function department()
{
return $this->belongsTo('App\DepartmentModel', 'id');
}
控制器
$departament = DepartmentModel::create ([
'department_name' => $request->department,
]);
SectionModel::create ([
'section_name' => $request->section,
'department_id' => $departament->id
]);
答案 1 :(得分:0)
尝试以下:
将表中“ id”列的数据类型字符串更改为$table->increments('id');
。
在“断面模型”中编辑关系方法:
public function department()
{
return $this->belongsTo('App\DepartmentModel', 'id');
}
在控制器文件中:
$departament = DepartmentModel::create([
'department_name' => $request->department
]);
$department->sections()->create([
'section_name' => $request->section
]);
答案 2 :(得分:-1)
这里是解决方案
User Table
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('firstname', 30);
$table->string('lastname', 30);
$table->string('email', 60)->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password', 191);
$table->string('phone', 15)->nullable();
$table->text('address')->nullable();
$table->string('town', 60)->nullable();
$table->integer('zip_code')->nullable();
$table->rememberToken();
$table->timestamps();
$table->engine = 'InnoDB';
});
Cart Table
Schema::create('carts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->unsignedInteger('product_id');
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->integer('quantity');
$table->timestamps();
$table->engine = 'InnoDB';
});