Datetime field in Laravel migration
Datetime field in Laravel migration
We’ll attempt to use programming in this lesson to solve the "Datetime field in Laravel migration" puzzle.
Create datetime field in Laravel using migration.You have to create a migration file after that you have to add a column using $table->dateTime('posted_at')->nullable();
code snippet. After changes in migration file you have to run php artisan migrate
command to create a datetime column type in database table.
-
Create datetime field in Laravel using migration
Create migration file using below command.
php artisan make:migration add_posted_at_column_to_posts_table --table=posts
Open created migration file under database/migrations folder and paste below code in up method.
$table->dateTime('posted_at')->nullable();
After implementation your code will look like below.
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('posts', function (Blueprint $table) { $table->dateTime('posted_at')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('posts', function (Blueprint $table) { // }); } };
Run the below command to migrate.
php artisan migrate
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- How to use bootstrap pagination in laravel 8
- There are no commands defined in the "route:" namespace
- How to display user profile after login in laravel
- How to call controller function from view in Laravel
- How to generate .env file for laravel?
- Laravel change date format
- How to add class to tr in table using foreach in laravel
- How to get specific columns using with method in laravel Eloquent relationship
- First and last item of the array using foreach iteration in laravel blade
- 419 page expired error in Laravel
- Send id with route Laravel
- How to run a specific seeder class in laravel
- How to get the random value form a specific column in laravel ?
- How to check duplicate entry in laravel
- How to get count of all records created at yesterday
- How to check email is valid or not in Laravel
- Get the post details if it has at least one comment in comments table
- Redirect from www to non www in laravel using htaccess
- Post table seeder laravel 10
- How to check data inserted or deleted in pivot after toggle method
- How to display a specific word from a string in laravel
- Remove several global scope from query
- Call to undefined method Illuminate\Support\Facades\Request::all()
- Multiple Level eager loading in Laravel
- Delete all related comments on deleting a post in Laravel