Skip to content

[9.x] Define a new method to create a through model - #41444

Merged
taylorotwell merged 1 commit into
laravel:9.xfrom
hafezdivandari:9.x
Mar 14, 2022
Merged

[9.x] Define a new method to create a through model#41444
taylorotwell merged 1 commit into
laravel:9.xfrom
hafezdivandari:9.x

Conversation

@hafezdivandari

Copy link
Copy Markdown
Contributor

It is helpful to have a customizable method for creating a new model instance for a through model without overriding the whole hasOneThrough or hasManyThrough methods. It is just as useful as newRelatedInstance method.

@taylorotwell

Copy link
Copy Markdown
Member

Can you describe your use case for this?

@hafezdivandari

hafezdivandari commented Mar 13, 2022

Copy link
Copy Markdown
Contributor Author

@taylorotwell sure, I wanted to pass the table name instead of the class and set the through model's table dynamically, something like this:

protected function newRelatedThroughInstance($resource)
{
    return (new \App\Models\Entity)->setTable($resource);
}

As I said in the comment above, the use case can be setting the model's connection or table conditionally/dynamically just like newRelatedInstance method.

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.

2 participants