Laravel Eloquent ORM - Get first and third table data - laravel

Category Table
Service Table
Branch Table
I have 3 tables link together. Above is my code, I want to produce the result same as below. Can I use a line of code to do that?
Or I have to get result from 2 tables first then only get the Branch table?
Thanks.

If you would like to obtain a collection of categories with service and branches you can eager load them in a single eloquent query.
$categories = Category::with('service.branches')->get();
But you still have to write a bit more if you would like to achieve your requirement.
Fractal Transformers might help you to do this. See their documentation here.

Related

Correct way to connect tables together in Laravel and retrieve in blade

I'm currently working on a project in Laravel.
I have 3 TABLES in MySQL.
Clicks - Costs - Profit
I want to show the data from these 3 tables in one blade.
What's the efficient way to achieve this?
For example: Having a shared same column value in each 3 tables, then returning the query with that value from every table.
Or maybe something like a belongsToMany Eloquent?
I think this is all you want - link
What is the database look like, do they share a foreign key? What data you explicitly want to extract and show to the blade?

Using Eloquent Laravel to show country's levels with big data

I have a table locations (about 35000 rows and more than in the future).
I search on the internet and try it: Eloquent: Recursive hasMany Relationship with Unlimited Subcategories
I try to run it, it kept loading and did not stop. I think so data is big.
In addition, the above solution shows the tree. I want to show the table
I want to set up a query using eloquent model or anything to pass to the view (can use DataTables package)
Thanks so much!

Laravel - How to relate two collections like Eloquent method "belongsToMany" and "with"

How can you combine two collections, one being the collection of parent items together combined with your collection of child items?
I would like something like using the method with and belongsToMany,but in this scenario I cannot use both methods correctly because one table is in another schema and the pivot table is in another schema.
Area::with('permissoes')
->where('sistema', '<>', 'S')
->get()
Most Eloquent eagerloads are done with separate queries that just use an IN statement on keys from the previous one. Pivot tables are the exception. It sounds like you need to explicitly tell the Model relation what database your pivot table is in. See my answer here: belongsToMany relationship in Laravel across multiple databases

How Eloquent ORM creates its data structure on eager loading

I have been looking into eloquent's source code to see how it manages building its data structure when eager loaded with one or more relationships specified but I have not been successful.
For instance:
User has many photos, and a photo belongs to a User
How does eloquent perform its SQL query?
Does it make multiple SQL? A query to get the user and then another one to get his/her photo, or does it perform a single query.
I am also interested to see the structure of the SQL, because I have tried lots of options and there is absolutely no way I could tell what tables each field belonged to because the returned result is just plain array of objects.
Laravel will create at least two queries when you'll try to use eager loading.
For example, this code:
$users = User::with('photos')->get();
Will produce two queries similar to these:
select * from `users` where `users`.`deleted_at` is null
select * from `photos` where `photos`.`user_id` in ('1', '2', ... '99')
I'd recommend you to use Laravel Debugbar, if you want to better understand how raw SQL queries look like.
Sometimes using toSql() in php artisan tinker tool is also helpful:
\App\User::where('id', 2)->toSql()

Laravel Eloquent model data from 2 tables

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?
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).

Resources