---
# System prepended metadata

title: 前置作業
tags: [CRUD, Init, Laravel, Login, First Project]

---

###### tags: `Laravel` `CRUD` `Login` `Init` `First Project`

> laravle 9.52.4
> PHP: 8.0.28
> mysql: 8.0.19

# 前置作業
- 建立資料庫
- 設置 .env file
```
APP_NAME=Laravel
APP_KEY=base64:aT07yFqfMPI8xs6skFGt8WRL3JQfX53LQQYrTCkPQQA=
APP_DEBUG=true
APP_URL=http://books.test

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=books
DB_USERNAME=root
DB_PASSWORD=root

CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailpit
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"
```

# Register
1. 建立 users table
- migration: remove `remember_token`
2. 建立 RegisterController
- `php artisan make:controller RegisterController`
- 建立 register function and return 'register'
- 建立 router
    - prefix with user
    - request method
    - endpoint
    - group
- postman: call register api: `POST /api/user/register`
    - body: json
    - header: Accept => application/json
- validate request input
    - required
    - string
    - int
    - min
    - max
    - email
    - alpha_num
    - confirmed
- Hash password
- create user
    - direct return user data
- abort if duplicate user
    - abort_if()
    - http status code: Illuminate\Http\Response::HTTP_BAD_REQUEST
    - error message: The email has already been taken.
    - translate: `__('auth.duplicate email')`
- use response()
    - `return response(['data' => $user]);`
    
# Login
- create AuthController
- create login router
- config/auth.php
    - defaults:
        - web -> change to api
    - create api guard
        - driver: jwt
        - provider: users
    - user provider:(keep)
        - driver: eloquent
        - model: App\Models\User::class
- Auth::attempt($credential)

# Logout
- create logout router
    - middleware: auth
- Auth::logout()
    - response: noContent() // status code:204

# Book CRUD

## user 新增一個角色欄位
- books 清單可以任意瀏覽
    - index: books list
    - show: id of book
- admin 可以編輯、刪除所有人的 book
    - store: create a book
    - update: update a book of all user
    - destory: delete a book of all user
- 一般角色只能編輯、刪除自己的
    - store: create a book
    - update: update a book
    - destory: delete a book
    
# User Email Verification

> [laravel document - Email Verification](https://laravel.com/docs/9.x/verification#introduction)

## Model Preparation
- User model implements MustVerifyEmail
## Registered Event Listener
`App\Providers\EventServiceProvider` already contains a `Illuminate\Auth\Listeners\SendEmailVerificationNotification` listener that is attached to the `Illuminate\Auth\Events\Registered` event
### usage:
in the regiser method
```
use Illuminate\Auth\Events\Registered;
 
event(new Registered($user));
```
## Database Preparation
- users table must contain an `email_verified_at` column

## 