Laravel 6.0 開始-2 Authority

# デフォルト認証機能利用

※npm run devでログ情報がクリアされたから、Commondのみ
(DB設定済み)

```
λ cd Lemuria-6\
λ composer require laravel/ui
λ php artisan ui vue --auth
λ npm install && npm run dev
λ php artisan migrate
```

OK

----

一層、複数のroleを追加します。

DB変更(Authorityカラムを追加):

```
λ php artisan make:migration add_authority --table=users
Created Migration: 2020_01_16_122719_add_authority
```

```diff:add_authority.php
class AddAuthority extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
+            $table->tinyInteger('authority');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
+            $table->tinyInteger('authority');
        });
    }
}
```

```
λ php artisan migrate
Migrating: 2020_01_16_122719_add_authority
Migrated:  2020_01_16_122719_add_authority (0.7 seconds)
```

ユーザ登録ができなくなりました。。

```sql
Field 'authority' doesn't have a default value
```

コード修正：

```diff:App\Http\Controllers\Auth\RegisterController.php
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
+            'authority' => 0,
        ]);
    }
```

```diff:App\User.php
    protected $fillable = [
-        'name', 'email', 'password',
+        'name', 'email', 'password', 'authority'
    ];
```

登録OK！

| id | name | authority |
|:-:|:-:|:-:|
| 1  |  TB | 0  |
|  2 |  TB2 | 1  |

0: 一般ユーザ
1: 管理者
という仮定します。

guard追加

```diff:config/auth.php

    'defaults' => [
-        'guard' => 'web',
+        'guard' => 'user',
        'passwords' => 'users',
    ],

    'guards' => [
-        'web' => [
-            'driver' => 'session',
-            'provider' => 'users',
-        ],

+        'admin' => [
+            'driver' => 'session',
+            'provider' => 'users',
+        ],

+        'user' => [
+            'driver' => 'session',
+            'provider' => 'users',
+        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
    ],

```

Admin画面をつくる

```
λ php artisan make:controller AdminHomeController
Controller created successfully.
```

```php:app/Http/Controllers/AdminHomeController.php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AdminHomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        return view('home', ['page' => 'Admin Home']);
        return view('home');
    }
}
```

```diff:app/Http/Controllers/HomeController.php
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
-        return view('home');
+        return view('home', ['page' => 'Home']);
    }
```

```diff:routes/web.php
Route::get('/home', 'HomeController@index')->name('home');
+ Route::get('/admin/home', 'AdminHomeController@index')->name('admin.home');
```

```diff:app/Http/Controllers/Auth/LoginController.php

+    /**
+     * Get the guard to be used during authentication.
+     *
+     * @todo return different guard
+     * @return \Illuminate\Contracts\Auth\StatefulGuard
+     */
+    protected function guard()
+    {
+        return Auth::guard('user');
+    }

```

```diff:resource/views/home.blade.php
-                <div class="card-header">Dashboard</div>
+                <div class="card-header">{{ $page }}</div>
```


つづく