本系列文的環境狀態,可點擊此連結後花園環境參考
[ 種花囉!園丁上場! ]章節的管理員系統 CRUD 仍是採 api 設定,有些部份在會員系統 CRUD 時已講過,可能就不再另外介紹。
另外,這次的做法是將管理員另外建一張 table 的版本,網上有其他教學是跟會員建在同一張 table ,然後有個欄位用代碼做管理員跟會員的區隔,看倌請依照自己的需求斟酌使用。
接下來,就讓我們可愛的園丁(管理員)來管管這些美麗的花兒(會員)吧!
可愛園丁(管理員)的創建使用流程:
設置 GardenerController 、 GardenerLoginController 、 Gardener 、 日期_create_gardeners_table
在 Terminal 輸入指令
$ php artisan make:controller GardenerController
$ php artisan make:controller GardenerLoginController
$ php artisan make:model Gardener -m
路徑:routes/api.php
// Gardener 註冊用
Route::post('/gardener','GardenerController@store');
// Gardener 登入用
Route::post('/gardener/login','GardenerLoginController@GardenerLogin');
// Gardener 驗證群組
Route::group(['middleware' => ['auth:gardener']], function () {
Route::get('/gardener/get', 'GardenerController@show'); // 查詢所有的花兒(會員)資料
Route::put('/gardener/put', 'GardenerController@update'); // 修改園丁(管理者)資料
Route::delete('/gardener/delete/{id}', 'GardenerController@destroy'); // 刪除花兒(會員)資料
});
路徑:config/auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
'flower' => [
'driver' => 'token',
'provider' => 'flowers',
'hash' => false,
],
'gardener' => [
'driver' => 'token',
'provider' => 'gardeners',
'hash' => false,
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'flowers' => [
'driver' => 'eloquent',
'model' => App\Flower::class,
],
'gardeners' => [
'driver' => 'eloquent',
'model' => App\Gardener::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
guards 填入 'gardener' 程式碼
'providers' 填入 'gardeners' 程式碼
路徑:database/migrations/ {日期}_create_gardeners_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateGardenersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('gardeners', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->timestamps();
$table->string('api_token')->unique();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('gardeners');
}
}
路徑:app/Gardener.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Gardener extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password','api_token'
];
protected $hidden = [
'password'
];
}
路徑:app/Http/Controllers/GardenerController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Gardener;
use App\Flower;
use Illuminate\Support\Facades\Auth;
use Str;
class GardenerController extends Controller
{
public function store(Request $request)
{
$request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'unique:gardeners'],
'password' => ['required', 'string', 'min:8','max:12'],
]);
$api_token= Str::random(10);
$Create=Gardener::create([
'name' =>$request['name'],
'email' =>$request['email'],
'password' => $request['password'],
'api_token' => $api_token,
]);
if ($Create)
return "園丁註冊成功...$api_token";
}
public function show()
{
return Flower::all(); // 查詢所有的花兒(會員)資料
}
public function update(Request $request)
{
$request->validate([
'name',
'email' => 'unique:users|email',
'password',
]);
Auth::user()->update($request->all());
return 'Gardener updated successfully';
}
public function destroy($id) // 需指定型態,用花兒(會員)的id 做辨識
{
$flower = Flower::where('id',$id);
if ($flower && $flower->delete()){
return '園丁已成功將花給處理掉了';
}
else{
return '未成功刪除';
}
}
}
路徑:app/Http/Controllers/GardenerLoginController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Gardener;
use Str;
class GardenerLoginController extends Controller
{
public function GardenerLogin (Request $request)
{
$gardener = Gardener::where([
'email' => $request -> GEmail,
'password' => $request -> GPassword
])->first();
$apiToken = Str::random(10);
if ($gardener->update(['api_token'=>$apiToken])) { //update api_token
return "園丁已成功登入, 你的 api token is $apiToken";
}
}
}
大功告成,園丁終於可以開始玩弄花兒們了???
嗯...咳...啊!不對,是管理花兒們啦!!!
完成後,可用 POSTMAN API 測試功能是否可以正常運行。
鑽研更多資訊,可參考下列連結:
❁ 手摸手教你讓 Laravel 開發 API 更得心應手
❁ Laravel 官方 - Authentication
❁ Laravel多使用者登入註冊(例:3個使用者,管理員,企業,普通使用者)