56
loading...
This website collects cookies to deliver better user experience
make:request
Artisan CLI command:php artisan make:controller UserController
app\Http\Controllers\UserController.php
. Then we define a method register()
in that controller that will perform all the instructions for registering a new user.public function register(){
//code...
}
PS: If you use PhpMyAdmin, create the database only with the database name, then in your .env
file assign the name of the database DB_DATABASE before running migrations. If it is the same as the project name, then you don't need to change it, it will be set to default already.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=qouteapp
DB_USERNAME=root
DB_PASSWORD=
make:request
Artisan CLI command:php artisan make:model User -m
create_users_table.php
in the migrations folder.Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('email')->unique();
$table->string('username');
$table->string('password');
$table->timestamps();
});
php artisan migrate
api.php
in the routes folder.use App\Http\Controllers\UserController;
Route::post('/user/register', [
UserController::class, 'register'
]);
make:request
Artisan CLI command:php artisan make:request UserRegisterRequest
app/Http/Requests/UserRegisterRequest.php
but if this directory doesn't exist this command creates one for you.authorize()
and rules()
by default.authorize()
public function authorize()
{
return true;
}
rules()
public function rules()
{
return [
'email'=>'required|email|bail|unique:users',
'username' => 'required|bail',
'password'=>'required|bail'
];
}
authorize()
and rules()
methods, we can do one more thing to make this customized.😍 Remember I said earlier that we can customize our error messages if any of our validation fails.messages()
method. This method should return an array of attribute/rule pairs and their corresponding error messages:public function messages()
{
return [
'email.required'=>'Email Is Required',
'email.email'=>'Enter a valid Email',
'email.bail'=>'Email Is Required',
'email.unique'=>'Email has been Taken',
'username.required'=>'Username Is Required',
'username.bail'=>'Username Is Required',
'password.required'=>'Password Is Required',
'password.bail'=>'Password Is Required'
];
}
UserController.php
.UserRegisterRequest
and a variable $request
are passed as a parameter in the register method.namespace App\Http\Controllers;
use App\Models\User;
use App\Http\Requests\UserRegisterRequest;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function register(UserRegisterRequest $request){
$user = new User();
$user->email=$request->input('email');
$user->username=$request->input('username');
$user->password= bcrypt($request->input('password'));
$user->save();
return response()->json([
'message'=>'Registration Successful'
]);
}
}
PS: We will have to set our header to accept JSON and if you'll be cloning my Github repository, I already created a middleware for that.
Adding After Hooks To Form Requests
Authorizing Form Requests(Restricting and authorizing certain users access rights)
Customizing The Validation Attributes
56