Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

I think you need to read up on what an interface is.

You are better of creating a repository that can access both models

Last updated 2 years ago.
0

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?

Last updated 2 years ago.
0

something like this

http://laravel.io/bin/X87

Last updated 2 years ago.
0

zenry said:

something like this

http://laravel.io/bin/X87

I notice on Line 22 you comment out finding in the b model. How does this then allow you to search both?

Last updated 2 years ago.
0

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:

  1. Merging results from two the tables
  2. Determining which table's results to prioritize for functions expecting a single model
  3. Creating and updating existing models

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.

Last updated 2 years ago.
0

zenry said:

something like this

http://laravel.io/bin/X87

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.

Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

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 :)

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.