Reputation: 5113
I've just started using Laravel and I'm coming from a different system using an existing database. In this system there are 2 users table, one stock with the CMS and one custom one.
I want to create an Eloquent model which retrieves data from both tables into one model. I know I can use the following to create a relationship.
$this->hasOne('App\SecondUser', 'id', 'id);
But this results in 2 sql queries, and I want to join results from 2 tables before returning the model, in one join
statement. How do I do this?
Upvotes: 0
Views: 938
Reputation: 1173
That might be a bit more complicated that you would expect.
First of all you have to use \DB
facade to join the two collections(arrays) and then recreate the Eloquent collection from these arrays using Collection's make
method.
More info about the Collection class here
An easier way might be to join them using standard laravel relationships and user something like Model::user->with('relation')->get
.
But this will still create 2 queries (still relatively fast).
Upvotes: 0