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

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!

Related

Laravel models, database and pivot tables question

Hello I am working with Laravel,
I have to create two simple models, let's say Stores and Books.
Stores can have one or multiple Books and Books can belong to many Stores.
Of course I will use a many to many relationship, with a pivot table.
Books the can have different prices depending the store.
I think a separate table can only complicate things, in my mind the pivot table associating books and stores should have a price column, but pivot tables only contains store_id and book_id.
Should I create a book_prices and associate it with books and to stores? What is the best approach?
You are free and able to set other attributes on your pivot table. You can read more about it in the docs.
https://laravel.com/docs/9.x/eloquent-relationships#retrieving-intermediate-table-columns
You have to define the relationship accordingly, the following should clarify how this works. In this example you use the many-to-many relationship and add the price column to every retrieved pivot model.
public function books()
{
return $this->belongsToMany(Book::class)
->withPivot('price')
}
For example, you are able to access the pivot column in a loop like this
foreach ($shop->books as $book)
{
echo $book->pivot->price;
}
You can define additional columns for your pivot table in the migration for the pivot table, and then when defining the relationship use withPivot to define the additional columns so they come through in the model:
return $this->belongsToMany(Book::class)->withPivot('price');
(Adapted from the Laravel documentation, see https://laravel.com/docs/9.x/eloquent-relationships#retrieving-intermediate-table-columns)
Depends on the complexity of your case, but yes, you have two options for it. Let's say that the pivot table is called as book_store:
Directly adds price column to book_store. This is obviously the simpler option. The drawbacks are:
The history of the price changes isn't logged. You'll have to create another table for logging if you want to keep this history information.
Changes made to price will directly change the price of the related book_store record. Meaning that a price is being updated "live" e.g users cannot update the price now but "publish" it some time later just like this example in the doc.
Create a new, different table to store the price. This may seems relatively more complex, but it may also be more future-proof.
Basically, you get 2 things that you miss in the first option above.
Don't think too much about book_store being a pivot table. One way to see it is like this: book_store IS a pivot table from books and stores tables viewpoints, but it's also just a normal SQL table which could relate to any other tables using any kind of relationships.
If you want to implement this, make sure to create a primary-key in the book_store table.
Alast, it all depends on what you need. Feel free to ask if you need more insight about this. I hope this helps.

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?

Eager loading relations for only a subset of records in a Laravel collection

I'm working on a legacy Zend 1 project that's in the process of migrating to Laravel via the Strangler Pattern, and I'm using Eloquent for some of the database queries (though not all as they're still in the process of being migrated). Because Zend 1 can't work on any version of PHP higher than 7.1, it's stuck on the version of Eloquent from Laravel 5.8 until further notice.
I have one particular table containing navigation items, which has a nullable polymorphic relation for content items that uses a morph map to map the Eloquent models to the item types. One type, static doesn't have an Eloquent model assigned to it since it's just a container for other navigation items. Trying to eager load the relation on any items with the static type breaks it because it's not defined in the morph map, and setting it to null doesn't resolve the issue either.
Right now I load the items with a link type of static in a separate query and merge them with the query that gets the remaining navigation items, but this means there are two separate queries and that's not great for performance. Using a union doesn't resolve the issue because the eager load is a separate query from the one for the navigation items.
I know that it's possible to call load() on a collection after it's already been retrieved via a query in order to eager load a relation, and that it's also possible to constrain the eager load so that, for instance, a WHERE clause can be applied when loading the relation. What I'm trying to find is a means to ensure that the query only attempts to eager load the relation if the item type is not static. In other words, constraining not the query to get the relation, but excluding that record from the query so no attempt is made to retrieve the non-existent relation for that record.
Does anyone know if this is possible? One solution I can think of is to split off the items that aren't static in collections and apply load() there, but that's rather cumbersome and I was hoping there was a more elegant method.

Laravel Eloquent ORM - Get first and third table data

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.

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