One To One Polymorphic Eloquent CRUD Laravel

Viewed 44

I have users table and 2 other tables with one to one polymorphic relationship a user can have any one type of profile.

when Users registered first time there is a radio button which select user type insert record into users table how do I add the morphs field into the database according to radio btn follow this doc Laravel 9

User model

public function userable()
{
    return $this->morphTo();
}

Employee Model

public function user()
{
    return $this->morphOne(User::class, 'userable');
}

Freelancer model

public function user()
{
    return $this->morphOne(User::class, 'userable');
}

How to perform CRUD on userable by user type there will be dropdown for user type

public function addUser()
{
    $user = new User();
    $user->name = 'John Doe';
    $user->email = 'test@gmail.com';
    $user->password = bcrypt('12345678');

    $user->type = 'freelancer' or 'employee';
    $user->userable()->associate(feelancer());

    $user->save();
    return $user;
}

User Table

Employees Table Employees Table

Freelancers Table Freelancers Table

0 Answers
Related