How to create Eloquent model with relationship

0 votes

How to create Eloquent model with relationship?

I have:

Person table

id
firstname
lastname

Employee table

id
person_id
position

I want to do something like this:

Employee::create([
'firstname' => 'Jack',
'lastname' => 'London',
'position' => 'writer'
])

I know, that can create two model and then associate their. But may be there is a way do this more beautiful?

Oct 28, 2020 in Laravel by kartik
• 37,520 points
484 views

1 answer to this question.

0 votes

Hello @kartik,

First, you have to create relation in your Person model

class Person extends Model
{
    protected $fillable = ['firstname', 'lastname'];

    public function employee()
    {
        return $this->hasOne('App\Employee');
    }
}

After that in your controller you can do:

$person = Person::create($personData);
$person->employee()->create($employeeData);

Hope it helps!!

Thank You!!

answered Oct 28, 2020 by Niroj
• 82,840 points

Related Questions In Laravel

0 votes
1 answer

How to create and Update Laravel Eloquent?

Hello @kartik, There has been a couple of ...READ MORE

answered Nov 11, 2020 in Laravel by Niroj
• 82,840 points
2,604 views
0 votes
1 answer

How can I create a migration to add a value to an enum in eloquent?

Hello @kartik, Try using this code: public function up() ...READ MORE

answered Nov 12, 2020 in Laravel by Niroj
• 82,840 points
2,544 views
0 votes
1 answer

How Laravel Eloquent Model Attributes maps to the table?

Hello @kartik, If your model is filled with ...READ MORE

answered Dec 1, 2020 in Laravel by Niroj
• 82,840 points
3,715 views
0 votes
1 answer

How to make a new page with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
13,090 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,080 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
3,016 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,948 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,885 views
0 votes
1 answer

How to create custom validation rules with Laravel?

Hii @kartik, Follow are the steps to create ...READ MORE

answered Mar 24, 2020 in Laravel by Niroj
• 82,840 points
1,224 views
0 votes
1 answer

How to manually create a new empty Eloquent Collection in Laravel?

Hello @kartik, It's not really Eloquent, to add ...READ MORE

answered Aug 11, 2020 in Laravel by Niroj
• 82,840 points
13,516 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP