将表的主键存储到另一个表的外键中-Laravel 5.8

时间:2019-08-08 07:08:11

标签: php laravel laravel-5

我想将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');
  }

3 个答案:

答案 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)

尝试以下:

  1. 将表中“ id”列的数据类型字符串更改为$table->increments('id');

  2. 在“断面模型”中编辑关系方法:

    public function department() 
    {
        return $this->belongsTo('App\DepartmentModel', 'id');    
    }
    
  3. 在控制器文件中:

    $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';
});