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
- If condition in Laravel 9
- Pass variable from blade to controller Laravel
- How to set column as primary key in Laravel model
- Delete all related comments on deleting a post in Laravel
- Call to undefined relationship [user] on model [App\Models\Post]
- Display message with session flash using bootstrap alert class in laravel
- How to send email in laravel
- How to get IP address in laravel
- Get Array of IDs from Eloquent Collection
- Convert multidimensional array to single array in Laravel
- Method Illuminate\Http\Request::validated does not exist
- Laravel 10 Breeze Authentication Example
- Route [password.request] not defined
- How to use more than one query scope in Laravel
- How to restore deleted records in laravel
- How to display user profile after login in laravel
- Rendering HTML from database table to view in Laravel
- Undefined property: stdClass::$title
- How to get the random value form a specific column in laravel ?
- Get comma separated email from input array
- Create project factory and seed data in laravel
- Touch parent updated_at in Laravel
- Return redirect laravel not working
- How to add columns in existing table using migration in laravel
- There are no commands defined in the "route:" namespace