Laravel Insert: created_at NULL or CURRENT TIMESTAMP?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today i want to show you a quick trick how to assign current value current timestamp on the created ad or updated ad and why would you need to do that so for example to create new data for performance reasons you don't use eloquent create but instead use insert for example we do that in csv import in our quick admin panel it's a trade that is used in many models that's why it's flexible with dollar model as a variable but basically how it works is you add a csv import you choose a file you choose csv which has two columns amount of transaction and transaction date so those two columns you choose the columns to assign to and for that import to be faster we don't use eloquent create we use eloquent insert like it would be for example transaction insert something like that the problem with that insert that is not eloquent insert is query builder insert which means for example that time stamps will not be automatically filled in created at an updated ad and that may be a problem so i want to show you a trick in migrations how to still use insert here but have default values directly from the migration so if we don't change anything so when we import the hundred rows it is in my csv and if we take a look at the database refresh we have transactions but without creating that unupdated ad and to change that in the migration in the original migration of that field of timestamps let's take a look what inside of those timestamps it's actually two timestamp method calls with precision which we don't really care about and both nullable let's override them instead of timestamps you can do instead of this timestamp of course it should be table timestamp and then we don't need the precision and instead of being nullable by default we will use a function called use current in the older versions of laravel you can find some tutorials where it is raw query of use current timestamp but at some point laravel introduced use current so we delete the timestamps we re-migrate the data so i re-migrate the data and those users and rows come from our quick admin panel so by the way if you want to generate something like this you can use quick admin panel.com and let's refresh we have 100 transactions seated with the seats and let's import another 100 we parse the csv we import the data so there are 200 transactions now and if we refresh the page so the first transactions were seated with the default seeder so that's eloquent with factories and then if we go below 100 we have this so this is done with use current and updated ad is still nullable because in our migration we have this so overall point if you use insert query builder and still want to define the created ad you can define use current directly in the migrations that's it a quick tip if you want more tips subscribe to the channel because i keep shooting daily videos on this channel and see you guys in those other videos
Info
Channel: Laravel Daily
Views: 11,699
Rating: undefined out of 5
Keywords:
Id: ZwtAAndwURA
Channel Id: undefined
Length: 3min 14sec (194 seconds)
Published: Wed Mar 23 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.