本系列文的環境狀態,可點擊此連結後花園環境參考
[ 種花囉! ]章節的會員系統 CRUD 將會是用 Api 放 POSTMAN 測試,所以不會用美美的前端教學,在此先給大家打個預防針。
然後,如果昨天有看我✾後花園D23✾文章的人,Route要更改下列部份內容,昨天有打錯。 |
---|
Route::delete('/flower/{api_token}', 'FlowerController@destroy'); |
今天就來 Controller 吧!請看倌們好好將它們納爲囊中之物吧!
路徑:app/Http/Controllers/FlowerController.php
<?php
namespace App\Http\Controllers;
use App\Flower;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Str;
class FlowerController extends Controller
{
// 註冊
public function store(Request $request)
{
$request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'unique:flowers'],
'password' => ['required', 'string', 'min:8','max:12'],
]);
$api_token= Str::random(10);
$Create=Flower::create([
'name' =>$request['name'],
'email' =>$request['email'],
'password' => $request['password'],
'api_token' => $api_token,
]);
if ($Create)
return "註冊成功...$api_token";
}
// 查詢
public function show()
{
return Auth::user();
}
// 修改
public function update(Request $request)
{
$request->validate([
'name',
'email' => 'unique:users|email',
'password',
]);
Auth::user()->update($request->all());
echo '資料修改成功,以下爲修改結果';
return $request->all();
}
// 刪除
public function destroy($api_token)
{
$flower = Flower::where('api_token',$api_token);
if ($flower && $flower -> delete()){
return 'Flower deleted successfully';
}
else{
return '未成功刪除';
}
}
}
今天就放上程式碼的部份,明天再來說碼解碼 function 內容物。
想知道更多,請自行連結:
[❁ Laravel 5.8 Tutorial: Build your First CRUD App](https://www.techiediaries.com/php-laravel-crud-mysql-tutorial/)
[❁ Laravel 建立 RESTful API](https://hackmd.io/@8irD0FCGSQqckvMnLpAmzw/Hk8QeMNLz?type=view)
[❁ Laravel CRUD實作(下)](https://hackmd.io/@8irD0FCGSQqckvMnLpAmzw/BkaOzFCMM?type=view)
[❁ CRUD in Laravel 5.8](https://bishrulhaq.com/posts/crud-in-laravel)