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 check records exist in loaded relationship in Laravel blade view
- Get id of last inserted record in laravel
- How to check data inserted or deleted in pivot after toggle method
- How to validate URL with https using regex in laravel
- Create project table with model and migration
- Touch parent updated_at in Laravel
- Method Illuminate\Database\Eloquent\Collection::lists does not exist
- Display message with session flash using bootstrap alert class in laravel
- Laravel route parameter
- Always load the relationship data with eager loading in Laravel
- Use withCount() to Calculate Child Relationship Records
- Target class [admin] does not exist.
- Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
- Remove array keys and values if it does not exist in other array in Laravel
- How to add background image to div using Tailwindcss, Vite in Laravel Environment
- Declaration of App\Models\Post::sluggable() must be compatible with Cviebrock\EloquentSluggable\Sluggable
- After image selected get validation error in laravel
- SQLSTATE[42000]: Syntax error or access violation: 1055
- Laravel 7 login error message not showing
- SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
- How to display HTML tags In Laravel blade
- JQuery each loop on json response after ajax in laravel
- Sample .htaccess file and index.php file under public directory in laravel
- Submit form without CSRF token in Laravel
- Setup laravel project with docker