[11.x] Allow factory to link to parent model without defining relationship model #51433
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Situation
When doing some testing in a project, I've face a thing which could happened in some situations.
Imagine we have a model
Computer
and another which isComputerReport
.Let's say we would like to build a view to show a list of every computers (
Computer
), and inside of a specific computer view, show all the linked reports (ComputerReport
). In this really simple situation, I think we'll have a relationship method like->reports()
on ourComputer
model, but I think it's useless to have a relation like->computer()
on theComputerReport
model as we'll never use it.The problem
When writing the testing logic :
We'll be facing the following problem :
Call to undefined method App\Models\ComputerReport::computer()
Because the logic just don't find the missing
->computer()
method in ourComputerReport
model.Solution
So, this PR make it work in this type of situation and don't force us to fill our model with logic which is only needed during testing. This is done though a
belongsTo
relation.