Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Add make method to HasOneOrMany and MorphOneOrMany relations #19307

Merged
merged 2 commits into from
May 24, 2017

Conversation

sebdesign
Copy link
Contributor

Implement a make method for instantiating eloquent models on hasOne, hasMany, morphOne, and morphMany relationships without performing inserts.

A similar example:

$post->comments()
    ->make(['body' => 'Hi there!'])
    ->addZondas()
    ->save();

This is heavily inspired from this sweet PR (#19015) from @calebporzio, who implemented a make method on the query builder.

sebdesign added 2 commits May 23, 2017 01:10
Implement a `make` method for instanciating eloquent models
on `hasOne`, `hasMany`, `morphOne`, and `morphMany` relationships
without performing inserts.
{
return tap($this->related->newInstance($attributes), function ($instance) {
$instance->setAttribute($this->getForeignKeyName(), $this->getParentKey());
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setAttribute returns the model. Why do we need the tap call?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just copied the create method and removed the save call. You are right, the tap is not needed, as the $instance can be chained through the setAttribute and then returned.

@taylorotwell taylorotwell merged commit 10e15da into laravel:5.4 May 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants