It seems like there should be a way to benefit from eloquent and also be able to sort everything out, but I'm not clear how. It seems that I need to remake my query and make the joins in the controller? That way I can sort it there, but I wouldn't be using the model functions on the view then. I see why it won't work but I'm not sure how to approach the solution. Also sorting Operation::find($id) makes no sense either since I'm only querying operation, I only get the info from the members in the view, when I'm doing the foreach loops. Yes, I reached the same conclusion about trying to sort the models. :/ĭoes the reasoning behind this make sense? You'd have to sort the results as mentioned by Ehesp in the second post, unless you want to adjust your relationships. Return $this->belongsTo('Hostess', 'hostess_id')->orderBy('lastname', 'desc') īut the above doesn't make sense because each Teammember belongsTo (meaning it is only related to one) Hostess. ![]() I think uldisrudzitis meant something like the following. Teams don't have a lastname, so that is the reason for the error. That returns the Operation's Teams ordered by lastname. Return $this->hasMany('Team')->orderBy('lastname', 'desc') If there is more than one element that should be returned, an \Illuminate\Collections\MultipleItemsFoundException will be thrown. ![]() Beware of N 1 issuesĬontinuing with our e-commerce example, let's say that we need to display the brand that each product belongs to.Change Operation.php content to public function team() If there are no elements in the collection that should be returned by the \Illuminate\Collections\ItemNotFoundException exception will be thrown. This technique is most effective in pages where you need to work with a ton of records, so you probably won't see a big change in memory usage/performance if you apply it to a page that fetches a few database records, nevertheless, it's still good to keep it in mind. This would make our query more efficient, this is especially important since the products' table would presumably contain a body column of type TEXT which could be quite large. You can reduce the amount of data by selecting only the columns you need: public function index() While doing this might seem reasonable (we are even paginating the results!), there may be a lot of information in our products' table that we don't need for a listing page (body, category id, product type, etc). ![]() ![]() 'products' => Products::query()->paginate() Our controller would look something like this: public function index() Let's say for example that we have an e-commerce website, and we want to display a list of products. The more data there is the more time it will take, but that's not all, all that data has to be stored in memory for the lifetime of the request and that can cause your server to slow down and run out of memory under heavy loads.įortunately, in Laravel, we can specify exactly the data we need. I'll give an example of what I want to do: I have table Operation (it's like an event), in each operation there will be many Teams. When you make a query, the data that the database returns is sent over the network. Eloquent makes my life easier and I don't have to care much about the queries, as long as my models are connected correctly but now I want to sort my queries and I don't know where to start. One way we can optimize a query is by reducing the amount of data we fetch from the database. Here I some techniques that can improve your load times in your Laravel application. Did you know that 1 in 4 visitors would abandon a website if it takes more than 4 seconds to load? While multiple factors can slow down a website, one common factor is inefficient database queries.Įloquent is a great tool that makes it easy to interact with the database in our Laravel applications, but sometimes we forget that what looks like a method call or property is running a database query under the hood, which might lead to slow page loads or high memory usage.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |