PHP + Laravel 雞排聯盟API實作: CRUD 增刪改查

by 班陳
Chicken_Affiliate

前一篇文章我們已經完成了可供操作的 Model 物件
及使用 migration 產出了相關 table
這篇文章就讓我們來看看要怎麼完成基本的 CRUD 增刪改查吧!

CRUD 增刪改查架構

讓我們複習一下目前的 route list (可以在command line 下php artisan route:list查看)

了解到 CRUD 增刪改查的對應關係如下

C 新增 Create

  • HTTP method: POST
  • URI: api/chickenFilletShop
  • 對應 controller: App\Http\Controllers\ChickenFilletShopController
  • 對應方法: store

R 查詢 Read

  • 範圍查詢
    • HTTP method: GET
    • URI: api/chickenFilletShop
    • 對應 controller:
      • App\Http\Controllers\ChickenFilletShopController
    • 對應方法: index
  • 指定 ID 查詢
    • HTTP method: GET
    • URI: api/chickenFilletShop/{chickenFilletShop} (這裡的{chickenFilletShop}可以當作是 id 來想)
    • 對應 controller: App\Http\Controllers\ChickenFilletShopController
    • 對應方法: show

U 修改 Update

  • HTTP method: PUT/PATCH
    (注意在使用 postman 打 api 時, form-data 不支持 put 和 patch,所以必須在 header 裡修改,這邊後面會再提)
  • URI: api/chickenFilletShop/{chickenFilletShop} (這裡的{chickenFilletShop}可以當作是 id 來想)
  • 對應 controller: App\Http\Controllers\ChickenFilletShopController
  • 對應方法: update

D 刪除 Delete

  • HTTP method: DELETE
  • URI: api/chickenFilletShop/{chickenFilletShop} (這裡的{chickenFilletShop}可以當作是 id 來想)
  • 對應 controller: App\Http\Controllers\ChickenFilletShopController
  • 對應方法: destroy

有了以上的關係架構後
我們就要去 controller 裡去完成各個方法的處理囉

C 新增 Create

app/Http/Controllers/ChickenFilletShopController.php

create

code

namespace App\Http\Controllers;

use App\ChickenFilletShop;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $requestx
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
            'type_id'     => 'required',
            'chain_store' => 'required|boolean'
            ]
        );
        $chickenFilletShop = ChickenFilletShop::create($request->all());
        return response($chickenFilletShop, Response::HTTP_CREATED); // 這邊不要忘記use Symfony\Component\HttpFoundation\Response;
    }

驗證

這邊先讓type_idchain_store是必填的
而且chain_store必須是布林值
更多 validate 相關方法可以看這邊: https://laravel.com/docs/5.8/validation#quick-displaying-the-validation-errors
若驗證失敗,Laravel會回傳錯誤訊息

  • ps. 若無法回傳錯誤訊息:
    • 解法:
      因為沒在header裡放Accept: application/json,加上這段在postman的header即可

說明

$chickenFilletShop = ChickenFilletShop::create($request->all());

  • Laravel 取得 body 內的資料 (存在 $request 內)
    使用all的方法取出後,藉由ChickenFilletShop這個 Model (上一篇文章建立的可供操作物件)去新增資料

    • ps. 這種使用方法有一個前提條件,就是 model 裡,已經有一個欄位名稱叫 ‘id’,Laravel 會依據 id 找到對應的 model 資料。

使用 Postman 測試

成功畫面如下:

create-success

R 查詢 Read

app/Http/Controllers/ChickenFilletShopController.php
本文先介紹最基本的,根據 id 來查詢的方法
下一篇文章再來敘述如何設定查詢條件、篩選、排序等

read

code

    /**
     * Display the specified resource.
     *
     * @param  \App\ChickenFilletShop  $chickenFilletShop
     * @return \Illuminate\Http\Response
     */
    public function show(ChickenFilletShop $chickenFilletShop)
    {
        return response($chickenFilletShop, Response::HTTP_OK);
    }

說明

show 傳入的物件 Laravel 會自動利用 Model 設定的主鍵去找出資料(主鍵預設id)

U 修改 Update

app/Http/Controllers/ChickenFilletShopController.php

update

code

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\ChickenFilletShop  $chickenFilletShop
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, ChickenFilletShop $chickenFilletShop)
    {
        $chickenFilletShop->update($request->all());
        return response($chickenFilletShop, Response::HTTP_OK);
    }

說明

由於 URI 是api/chickenFilletShop/{chickenFilletShop}
其中的{chickenFilletShop}就是 id
Laravel 會根據這個 id 去 ChickenFilletShop 這個 Model 去找對應的資料
並使用$request->all()來取出送過來的資料,以執行 update() 最後回傳已更新的 $chickenFilletShop

HTTP方法注意點:

使用 Postman 測試

chain_store,也就是紀錄該店是否為連鎖店的欄位
由 1 改為 0

PATCH

D 刪除 Delete

app/Http/Controllers/ChickenFilletShopController.php

delete

code

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\ChickenFilletShop  $chickenFilletShop
     * @return \Illuminate\Http\Response
     */
    public function destroy(ChickenFilletShop $chickenFilletShop)
    {
        $chickenFilletShop->delete();
        return response(NULL, Response::HTTP_NO_CONTENT);
    }
}

說明

由於 URI 是api/chickenFilletShop/{chickenFilletShop}
其中的{chickenFilletShop}就是 id
Laravel 會根據這個 id 去 ChickenFilletShop 這個 Model 去找對應的資料
並執行 delete() 最後回傳 NULL

使用 Postman 測試

成功的話,照目前設定會回傳空值
且db內該筆資料會被清掉

delete

db顯示為空

blank-db

好的! 這樣我們就完成了基本的CRUD
下一篇再來介紹怎麼在查詢時加入各種條件
有任何問題的話也歡迎隨時留言喔~!

系列文章

最後附上系列文章

  1. PHP + Laravel 雞排聯盟API實作: 前言
  2. PHP + Laravel 雞排聯盟API實作: 產出 table 及可供操作的物件
  3. PHP + Laravel 雞排聯盟API實作: CRUD 增刪改查

以及初學時看的書 推薦!

You may also like

Leave a Comment