Laravel Migrations: 12 Useful Tips in 12 Minutes
ฝัง
- เผยแพร่เมื่อ 7 ก.พ. 2025
- In this video, I decided to share less-known things I've learned about Laravel migrations while working with Laravel over the years.
- - - -
Support the channel by checking out my products:
My Laravel courses membership: laraveldaily.t...
Laravel QuickAdminPanel: bit.ly/quickad...
Livewire Kit Components: livewirekit.com
- - - -
Other places to follow:
My weekly Laravel newsletter: bit.ly/laravel-...
My personal Twitter: / povilaskorop
00:10 - Foreign Keys in Laravel 7 and below
01:35 - constrained() and nullable()
02:15 - Delete Parent: Restrict or Cascade?
04:10 - Change Order of Migrations
05:05 - What Migrations Have (not) Run?
05:40 - Timestamps with Default Value
06:23 - Customize Default Migration File
07:28 - Export Migrations into One SQL File
08:38 - Drop Multiple Columns
09:05 - Rollback or Refresh X Steps
09:57 - Auto-increment starting value
10:45 - Make Migration: Spaces vs Underscores
You keep producing videos that are worthy of Oscar Award every time.
Man! You are the real deal
agree
Very clean and clear. What I love most about your videos is that that theirs no ambiguity at all. You're the best.👍
Thank you so much for contributing to the Laravel community! It's high quality content, well spoken, and I quite enjoy the topic as well. Farewell!
You are a gem in the Laravel community
Respect
You are the best teacher of Laravel, Thank you!
Thank you for sharing these tips! Your channel videos are simply amazing, simple, extremely useful, and inspiring.
Thanks, I have learned new tricks I didn't know about.
😍 i realy love your tips , and hope to be able to pay some day for those courses , tyvm for all the hard work
Your videos are always worth watching. Thank you.
01:35 I learnt it the hard way. Edit: Thanks, I learned a lot!
Your videos are so helpful and I'm learning so much from them, thanks a lot!
Short tip and I know soft delete exist. Thanks!
Another thing that may be helpfull - add unique number for migration class and name:
php artisan make:migration users_fix_`date +%N`
this will add nanoseconds value to migration name and class name making it abolutly unique.
Work in unix systems only.
You can also use camelCase on migrations and it will add _ and lowercase letter on each capital... I think that's even faster
great content
also for migration names you can use:
php artisan make:migration AddFieldsToUsersTable
and it will add underscores
im happy that i use the same rename method to change the order of migration and i dont have evern 1 year of laravel experience
Another usefull video , Thank You!
Very Informative 👏👏
*Nice tips I learned a few things, thanks!*
More of these tips 👍
Awesome teaching
Great content, tyvm.
Incredible tutorials, thankyou so much.
Very useful, as always. Thank you very much!
Great content as always
Love you so much sir ❤️❤️
thank you so much your tutoreil it's very helpful
Great video
really appropriate thanks
you are the best
Man, i love you! serious
Thanks! Very useful tips!
Sir, your forgotten one of most typical Junior developer mistakes, they do any migration mutations in existing migration files, although they must do in seperate files.
any video for this?
I love your channel :)
Thanks you Sir for the awsome tips. I have a question on staps. In migrations sometimes we create for adding or removing colums or other, so at that time we dont need softDeletes is there any option to avoid softDeletes of other custom fields when we creating migration for columns?
Great sharing, thanks!
Which IDE are you using? And what’ extension?
Hello, what is the extension to be able to visualize in the code if it is a table or a column? thanks for your tutorials!
I would additionaly tell about "--table" and "--create" parameters in php "artisan make:migration". And maybe "php artisan make:model SomeModel -m"
Awesome, Very Useful!
10:25 "Six million dollar man" mode
Thanks for another one 😊
dear sir, will you create a tutorial about Alibaba E-commerce website API integration ?
Can you do a video updating Laravel 7 to 8
Very helpful 👍😍
Thank you for this video. I know you have covered this elsewhere, but I can't recall - how do you add foreign keys to existing columns. For e.g. I have a table which is like user_id but I did not mention it was a foreign key at first. Is there a way to retroactively add that in?
You can create a new migration file and add $table->foreign('user_id')->references('id')->on('users');
But you need to make sure that current data does not violate that constrain, that there's no record with non-existing user_id.
@@LaravelDaily So nullable cannot be applied in this way?
@@coolcha It can be applied, but if you already have values that don't have corresponding parent values, then nullable won't help.
@@LaravelDaily I see. Thank you for all your guidance and help.
Dear Povilas!
Do you have Laravel lessons series about scaling?
For example, if you're doing complex social network (like TH-cam clone), how to scale it infinitely?
Hi, scaling is a very very individual thing, depending on the project and actual problems to solve: is it a DB issue? is it a Laravel code optimization issue? is it just adding more servers? etc. So it's very hard to create any kind of "definite" lessons on this topic. Maybe in the future I will come up with something.
Thank you povilas
Thanks you !
Thanks a lot It help me lot
Very helpful. Thanks
If i want to change the column type, like string to datetimes from my database table, what is the best solution for this?
Great content, 1 question, should I use key type based on the project size, like integer for small and big integer for larger project... or use biginteger everywhere like default. Is it worth trading some performance for faster coding
Thank Sir
When to use constrained() on a foreign key?🤔
This is excellent
fine
Surpise there's no mention of $table->foreignIdFor($model)
It's not really widely used, even Taylor mentioned once on Twitter that he's not a fan of it, because model may change and then it breaks.
Can u make video on localization for both vuejs and blades files for multi language vue application which is also running with blades features .
This is how we use it: th-cam.com/video/pYtzXPrigKE/w-d-xo.html
nice job new abonnée
Great tips ...
wow, great!!!
sry to bother you my master teacher i have a question
when i write the following command
$table->foreignid('table_name')->constrained('table_name');
should i put it right after the foreign key table or in the primary key migration file
if the first, how can i tell the laravel which primary key from which table to constrain ?
laravel 8 version
thanks in advanced .....
Not sure I understand your question, please read the official documentation laravel.com/docs/8.x/migrations#foreign-key-constraints
Can you help me, when i do the 'php artisan migrate' it doesn't migrate any file from database/migrations. And when i check at my database there are only Migrations table created in it. I tried the migrate:rollback but it show 'nothing to rollback', i tried the php artisan migrate again and again but it just showing 'nothing to migrate'
Maybe you have put migrations innwrong folder? Or maybe you've called the classes/methods in nonstandard way so Laravel doesnt recognize them?
@@LaravelDaily I dont think so because i just created this new project and follow a tutorial from the beginning, and their migration works but mine dont.
Sorry, I can't debug it for you.
@@LaravelDailyIt's okay, Thanks for replying🙏🏾
Is there a way to do a cascading soft delete?
Yes. Here's my article: blog.quickadminpanel.com/one-to-many-with-soft-deletes-deleting-parent-restrict-or-cascade/
can we export migration and seeder into one sql file?
Yes, but not from Laravel, from your database client Export function.
I am not fan of saving few milliseconds while not typing the underscore, but I'm a fan of not breaking my fingers, since for some reason I find it kinda difficult to type underscore with one hand :/
Foreign constraints give me headaches 🤯
I collect data from the movie db api and EVERY key value can be missing...
I had every record linked to a parent... Took me ages to get right.
I had to remove ALL the constraints for seasons because many TV shows fails the queue job.
At least I still have the TV constraint to remove everything belonging to that 😖
Thanks
Please turn on the subtitles
i hate timestamps.. because? there is a limit i found out that in mysql at least timestamp() will stop working from year 2038 or something... so maybe your system/app wont be around but w/e..
fantastic
will we lost data by rollback sir??
Yes if you delete the table/columns on rollback
@@LaravelDaily how to update migration files... like Add columns or modify datatypes without loss table data sir??
Can u make video laravel-websockets
I don't work with websockets currently, so can't make a video about it.
Great
How come that I get to know new things even though I'm from Symfony camp? O_o
can you make a video with use withMax() on pivot table price column?
Good suggestion, will do.
@@LaravelDaily ok thankyou, all i know withMax() is can't used on pivot table, i tired use
user_id | project_id | rating
User::withMax('projects', 'project_user.rating')
it seems work, but the sql look weird ;(
and also tired create a new model method user hasMany project relate to the pivot table,
user_id | project_id | rating
just don't know any better way to do this
Great video. Thanks!
Thank you!
Thanks
Great
Great