New in Laravel 11 - The Once Method

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys welcome to Lara tips laravel 11 is released and there are so many new features in laravel and among so many features of LEL 1 today I'm going to talk about the once method now let's see what it does and how can we use it so currently I'm here in the fresh larel project and I have done very minor changes over here suppose let's say we are getting a new user from the database and if you see here in the users table I have created this first user method and I am returning this one and inside the ones we have passed the new closure like this but before this let me just do it like this just return the first user okay let's say we are calling this first user method from different parts of the application so currently I'm just adding it over here so if we do it like this and let me enable the query logging over here and it will just log whatever the query that has ran in between this and this here I'm getting the first user and again I'm getting the first user so it is just returning getting the user from the database and returning it so now if I come here and refresh then you can see here the same query has run twice which means that two query has been run but we only want this query to be run once and get the results so for that we can use this once method that LEL provides so here we will pass a closure inside the ones and then we'll pass whatever thing that we have done over here now if I just refresh it then you can see here it is only being run once which means that how many times we call that method the database is hit only once and I really like this feature and obviously I'm going to use this in many part of my application but now let's see how we can do a basic implementation of this one's functionality not by creating a separate Helper but within the class itself so I have this first user property okay I have added this so which will be a user and now here I am doing like this so this is the user one property okay so if the user property is not set we are setting it by using the user first by getting this from the database and now if we again run this code then database query will be run only once because whenever we call this like this then it will P the user from the database and assign it to this user one method and whenever we call it second times the user one is already set so it will get it from this so now if I just refresh then you can see here it is only being called once and this is the query so that's it for this video guys thank you for watching have a great day bye
Info
Channel: Laratips
Views: 5,125
Rating: undefined out of 5
Keywords: laravel for beginner, laravel beginner tutorial, laravel tutorial for beginners, laravel tutorial, laravel advanced, laravel advanced tutorial, php, php tips, php tips and tricks, tips and tricks, laratips, laravel, laravel tips, laravel 10, laravel 10 tutorials, whats new in laravel 10, laravel 11, new in laravel 11
Id: 1Lhc-OA-zWg
Channel Id: undefined
Length: 2min 33sec (153 seconds)
Published: Thu Mar 14 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.