
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
- RuntimeException You must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
- Update email with unique validation in laravel
- Store logged in user details in session and display in view in laravel
- How to get last record from object collection in laravel
- How to decrypt laravel password
- How to add a key value pair to existing array in laravel
- Print query in laravel
- The use statement with non-compound name 'DB' has no effect
- How to restore multiple records after soft-deletes in Laravel
- How to check find method executed successfully in laravel
- How to fetch single row data from database in laravel
- Php artisan make model, factory, migration and controller in single command
- How to check query string exists or not in laravel blade
- Update if exist else insert new record in laravel
- There are no commands defined in the "route:" namespace
- Laravel 7 login error message not showing
- Create records using relationship in laravel
- Global scope in Laravel with example
- How to prevent host header attack in Laravel
- How to add foreign key in laravel using migration
- Trying to get property 'title' of non-object
- How to get specific columns using Laravel eloquent methods
- How to use bootstrap pagination in laravel 8
- How to restore deleted records in laravel
- How to add columns in existing table using migration in laravel