laravel date default now 1

laravel date default now

$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));

Here is what the above code is Doing:
1. We’re creating a table called ‘users’
2. We’re adding a column called ‘id’ that is an auto-incrementing integer and is the primary key of the table
3. We’re adding a column called ‘name’ that is a string of 255 characters
4. We’re adding a column called ’email’ that is a string of 255 characters
5. We’re adding a column called ‘password’ that is a string of 255 characters
6. We’re adding a column called ‘created_at’ that is a timestamp

Now, let’s run the migration:

$ php artisan migrate

Now, let’s check the database to see if the table was created:

$ sqlite3 database/database.sqlite

sqlite> .tables

migrations users

sqlite> .schema users

CREATE TABLE “users” (“id” integer not null primary key autoincrement, “name” varchar(255) not null, “email” varchar(255) not null, “password” varchar(255) not null, “created_at” datetime not null);

sqlite> .quit

Now, let’s add some users to the database:

$ sqlite3 database/database.sqlite

sqlite> insert into users (name, email, password) values (‘John Doe’, ‘john@example.com’, ‘password’);

sqlite> insert into users (name, email, password) values (‘Jane Doe’, ‘jane@example.com’, ‘password’);

sqlite> .quit

Now, let’s create a route to display the users:

$ php artisan make:controller UserController

Now, let’s add the following code to the UserController:

$users]);
}
}

Now, let’s create a view to display the users:

$ touch resources/views/users.blade.php

Now, let’s add the following code to the users.blade.php file:




Users

Users

    @foreach ($users as $user)

  • {{ $user->name }}
  • @endforeach


Now, let’s add a route to the routes/web.php file:

Route::get(‘/users’, ‘UserController@index’);

Now, let’s start the server:

$ php artisan serve

Now, let’s go to the following URL in the browser:

http://localhost:8000/users

You should see the following:

Users

John Doe
Jane Doe

Now, let’s create a route to display a single user:

$ php artisan make:controller UserController

Now, let’s add the following code to the UserController:

Similar Posts