I think you need to read up on what an interface is.
You are better of creating a repository that can access both models
Zenry, sorry, I used the word Interface not as the technical term, but a way to interface with it - my bad.
Do you have any examples of creating a repository that does such a thing?
something like this
zenry said:
something like this
I notice on Line 22 you comment out finding in the b model. How does this then allow you to search both?
Depending on the complexity of your functionality, this might not be as difficult as it first appears. The primary issues you have to consider are:
Issue #1 should be trivial to solve, since the collection already has a merge function:
public function all()
{
$collectionA = $this->modelA->all();
$collectionB = $this->modelB->all();
return $collectionA->merge($collectionB);
}
I assume you've already handled Issue #2 in some way in your existing codebase.
Issue #3 may be the most annoying to solve, but you should still be able to solve this relatively easily as long as you know which table you're updating. I'd probably do this via a hidden form attribute:
public function create(array $input)
{
//you should probably have something a bit more elegant than 'B'
$modelB = (isset($input['table']) && $input['table'] == 'B') ? false : true;
unset($input['table']);
if($modelB) {
return $this->modelB->create($input);
} else {
return $this->modelA->create($input);
}
}
Updating your models shouldn't be a problem, because as long as you have an ID you already know what table it's going to be saved to.
zenry said:
something like this
the comment is there because you cannot have 2x return in your methods (basic php ;))
the example is merely to demonstrate the use of a repository, your controller can talk to your repository and the repository decides what info to return from whatever model you need.
just remember that Laravel is PHP, als long as you load your classes (composer or laravel's autoloader) you can place the files where-ever you like, name them whatever and use namespaces.
I for example got rid of the models
directory, find it to generic and I'm currently building everything in my own namespace
zenry said: the comment is there because you cannot have 2x return in your methods (basic php ;))
Oh, I do know that — I was merely wondering how I could merge them together completely forgetting about the merge
method.
Thanks for your help thepsion5, I'll have a play :)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community