使用网桥表(laravel雄辩)将信息存储到规范化数据库表中

时间:2019-04-27 12:01:33

标签: mysql laravel

Here is my ER diagram 我有3个数据库表用于存储员工数据。我想将员工信息存储到这些表中。我正在使用一个桥接表调用employee_address来存储这些雇员的地址。该地址表还保存了供应商地址的地址。如何存储这些数据。

我的控制器代码

 public function store(Request $request)
    {

        $employee = new Employee();

        $employee->employees_first_name = $request->input('first_name');
        $employee->employees_last_name = $request->input('last_name');
        $employee->employees_dob = $request->input('date_of_birth');

        $emp = Employee::findOrFail(1);
        $emp_address = new Employees_address();
        $address = new Address();

        $address->address_postal_code = $request->input('postal_code');
        $address->address_house_no = $request->input('house_no');
        $address->address_road_no = $request->input('road_no');
        $address->address_thana = $request->input('thana');
        $address->address_district = $request->input('district');
        $emp->address()->save($emp_address);
    }

我的员工模型

class Employee extends Model
{
    protected $fillable = [
        'employee_first_name', 'employee_last_name',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [

    ];
    public function address(){
        return $this->hasOne('App\Address');
    }
}

0 个答案:

没有答案