WebDevelpment-Lec 9

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

Laravel Database Migration

&
Authentication

Lecture 9
Migration
❑ Migration provides a way for easily sharing the schema of the database. It also makes the
modification of the schema much easier.

❑ It is like creating a schema once and then sharing it many times.

❑ It gets very useful when you have multiple tables and columns as it would reduce the work over
creating the tables manually.

❑ Create Migration: It can be created by using the artisan command as shown below:

❑ php artisan make:migration create_articles_table


articles
❑ articles are going to be the table and in place of that you can write any other
table name which you want to create and is appropriate for the application but the
table name as you saw, is to be in plural form.
❑So, it is written as articles and not article. This is the naming scheme used by
Laravel and it is important to specify create at the start and table at the end
according to the naming scheme of a Migration file in Laravel.
❑ All the migration file that we create using the artisan command are located
at database/migrations directory. So, after we run the above command, it will
generate a PHP file with the name we specified with the current date and time.
Basic Structure of a Migration
▪ you want to specify the name of the table different than the name that you specified
as the file name then you can use an option as –create with the command as follows:
php artisan make:migration create_articles_table --create=gfg
With this command, the table name contained in the create() method will be gfg and
not articles.
▪A migration file contains a class with the name of the file specified while creating
the migration and it extends Migration.
Basic Structure of a Migration
❑ In that we have two functions, the first one is up() function and the
second one is down() function.
❑ The up() is called when we run the migration to create a table and
columns specified.
❑ in the up() function, we have create method of the Schema facade
(schema builder) and as we say before, the first argument in this function
is the name of the table to be created. The second argument is a function
with a Blueprint object as a parameter and for defining the table.
Basic Structure of a Migration
❑ The ‘down()’ function is called when we want to undo the creation
of ‘up()’ function.
❑ In the down() function, we have a dropIfExists method of the
schema builder which when called will drop the table.
Create table example:
◦ public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}

◦ This will create a users table with the following columns: id, name, email, email_verified_at, password,
remember_token, and timestamps.
To Run Migration:
o Before running a migration, we first have to create a MySQL Database and Connect to it.
o After that is done, to Run a Migration, we can use an Artisan command as follows:
o php artisan migrate
Rollback Migration
❑ Thiscommand will run the up() function and create all the tables in the
database for all the migration file in the database/migrations directory.
❑To rollback any last migration which is done, we can use the following
Artisan command:
php artisan migrate:rollback
Laravel Authentication
An Introduction to Laravel Authentication

❑ Authentication is one of web applications’ most critical and essential


features. Web frameworks like Laravel provide many ways for users to
authenticate.
❑You can implement Laravel authentication features quickly and
securely. However, implementing these authentication features poorly
can be risky, as malicious parties can exploit them.
❑Laravel introduces modules that are made up of “guards” and
“providers.”
❑Guards define user authentication for each request, and providers define
user retrieval from persistent storage (e.g. MySQL database).
An Introduction to Laravel Authentication
➢ First, you have to define the authentication defaults.
➢ This option controls your application’s default authentication “guard” and password reset options.
You may change these defaults as required, but they’re a perfect start for most applications.
➢ First, you have to define the authentication defaults. This option controls your application’s default
authentication “guard” and password reset options. You may change these defaults as required, but
they’re a perfect start for most applications.
➢ Next, you define authentication guards for your application. Here, our default configuration uses
session storage and the Eloquent user provider. All authentication drivers have a user provider.
How To Implement Laravel Authentication
Starting with registering users and creating the needed routes in
routes/web.php
We will create two routes, one to view the form and one to register:
use App\Http\Controllers\Auth\RegisterController;
use Illuminate\Support\Facades\Route;
/* Web Routes Register web routes for your app's RouteServiceProvider in a group
containing the "web" middleware */
Route::get('/register’, [RegisterController::class, 'create']);
Route::post('/register', [RegisterController::class, 'store']);
➢ The controller is empty now and returns a view to register.
➢Let’s make a view in resources/views /auth
➢And call it in register.blade.php
➢ now with everything in place, we should visit our register route and see the
following form:
❑ Now that we can display a form that a user can complete and get the data for
it, we should get the users’ data, validate it, and then store it in the database if
everything is fine.
❑ Here you should use a database transaction to ensure the data you insert is
complete.
❑ We will use Laravel’s request validation feature to ensure that all three
credentials are required.
❑ We have to make sure the email has an email format and is unique in the
users table and that the password is confirmed and has a minimum of eight
characters:
Validate input :
Now that our input is validated, anything going against our validation will throw an error that
will be displayed in the form.
Remembering Users
Most, if not all, modern web applications provide a “remember me” checkbox
on their login form.
If we want to provide a “remember me” functionality, we may pass a boolean
value as the second argument to the attempt method.
When valid, Laravel will keep the user authenticated indefinitely or until they are
manually logged out. The user table must include the string:
Remember_token
First things first, you have to add the Remember Me field to your
form:
And after this, get the credentials from the request and use them on the attempt method on
the Auth facade.
If the user should be remembered, we will log him in and redirect him to our homepage.
Otherwise, we will throw an error:
Resetting Passwords

❑ Most web applications today provide ways for users to reset their passwords.
❑ We will make another route for the forgotten password and create the controller as
we did. Additionally, we will add a route for the reset password link that contains the
token for the entire process:
Route::post('/forgot-password', [ForgotPasswordLinkController::class, 'store’]);
Route::post('/forgot-password/{token}', [ForgotPasswordController::class, 'reset']);

❑ Inside the store method, we will take the email from the request and validate it as we
did.
Resetting Passwords (cont’d)

After this, we can use the SendResetLink . method from the password facade.
And then, as a response, we want to return the status if it succeeded in sending the link or errors
otherwise:

'email' => __($status)])


Resetting Passwords (cont’d)

❑ Now that the reset link has been sent to the user’s email, we should
take care of the logic of what happens after that.
❑ We will get the token, email, and new password in the request and
validate them.
❑ After this, we can use the reset method from the password facade to let
Laravel take care of everything else behind the scenes.
❑ We are always going to hash the password to keep it secure.
❑ In the end, we will check if the password was reset, and if it were, we
will redirect the user to the login screen with a success message.
Otherwise, we display an error that it could not be reset:
;
Thank You
&
Good Luck in
your Projects

You might also like