{"id":338,"date":"2018-02-28T05:17:01","date_gmt":"2018-02-27T20:17:01","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=338"},"modified":"2018-02-28T05:17:01","modified_gmt":"2018-02-27T20:17:01","slug":"%e3%81%9f%e3%81%a3%e3%81%9f%ef%bc%93%e3%82%b9%e3%83%86%e3%83%83%e3%83%97%ef%bc%81laravel-5-6%e3%81%a7restful-api","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=338","title":{"rendered":"\u305f\u3063\u305f\uff13\u30b9\u30c6\u30c3\u30d7\uff01Laravel 5.6\u3067RESTful API"},"content":{"rendered":"<p>\u73fe\u5728\u306e\u958b\u767a\u3067\u7c21\u5358\u306a<strong>RESTful API<\/strong>\u306e\u958b\u767a\u3092\u3057\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001Laravel\u3092\u4f7f\u3046\u3068\u9a5a\u304f\u307b\u3069\u7c21\u5358\u306b\uff08\u4f5c\u696d\u3092\u7701\u7565\u3057\u3066\uff09\u5b9f\u88c5\u3092\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u305d\u3053\u3067\u3001\u4eca\u56de\u306fLaravel 5.6 \u3092\u4f7f\u3063\u3066\u3001<\/p>\n<ul>\n<li><strong>\u95b2\u89a7<\/strong><\/li>\n<li><strong>\u8ffd\u52a0<\/strong><\/li>\n<li><strong>\u7de8\u96c6<\/strong><\/li>\n<li><strong>\u524a\u9664<\/strong><\/li>\n<\/ul>\n<p>\u304c\u3067\u304d\u308bAPI\u3092\u4f5c\u6210\u3059\u308b\uff13\u30b9\u30c6\u30c3\u30d7\u3092\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<h1>\u524d\u63d0<\/h1>\n<p>\u3059\u3067\u306bUser\u3068\u3044\u3046\u30e2\u30c7\u30eb\u304c\u4f5c\u6210\u6e08\u307f\u3068\u3044\u3046\u524d\u63d0\u3067\u8a71\u3092\u9032\u3081\u307e\u3059\u3002<\/p>\n<h1>\uff11\uff0e\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u4f5c\u6210<\/h1>\n<p>Laravel\u306b\u306f\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u3092\u81ea\u52d5\u4f5c\u6210\u3059\u308b\u30b3\u30de\u30f3\u30c9\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u5b9f\u306f\u3053\u306e\u30b3\u30de\u30f3\u30c9\u306b\u30aa\u30d7\u30b7\u30e7\u30f3\u30fb\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u8ffd\u52a0\u3059\u308b\u3060\u3051\u3067\u3001<strong>RESTful API<\/strong>\u306b\u5fc5\u8981\u306a\u57fa\u672c\u30b3\u30fc\u30c9\u3092\u6e96\u5099\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><strong>php artisan make:controller UserController --resource --model=User<\/strong><\/pre>\n<p>\u8aac\u660e\u3068\u3057\u3066\u306f\u3001<\/p>\n<ul>\n<li><strong>&#8211;resource<\/strong>\u3000\u30fb\u30fb\u30fb\u3000API\u7528\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u81ea\u52d5\u3067\u8a18\u8ff0<\/li>\n<li><strong>&#8211;model=*****<\/strong>\u3000\u30fb\u30fb\u30fb\u3000\u3055\u3089\u306b\u5404\u30e1\u30bd\u30c3\u30c9\u306b\u30e2\u30c7\u30eb\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u6e96\u5099\u3057\u3066\u304f\u308c\u308b<\/li>\n<\/ul>\n<p>\u5b9f\u969b\u306e\u4e2d\u8eab\u3068\u3057\u3066\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Http\\Controllers;\r\n\r\nuse App\\User;\r\nuse Illuminate\\Http\\Request;\r\n\r\nclass UserController extends Controller\r\n{\r\n    \/**\r\n     * Display a listing of the resource.\r\n     *\r\n     * @return \\Illuminate\\Http\\Response\r\n     *\/\r\n    public function index()\r\n    {\r\n        \/\/\r\n    }\r\n\r\n    \/**\r\n     * Show the form for creating a new resource.\r\n     *\r\n     * @return \\Illuminate\\Http\\Response\r\n     *\/\r\n    public function create()\r\n    {\r\n        \/\/\r\n    }\r\n\r\n    \/**\r\n     * Store a newly created resource in storage.\r\n     *\r\n     * @param  \\Illuminate\\Http\\Request  $request\r\n     * @return \\Illuminate\\Http\\Response\r\n     *\/\r\n    public function store(Request $request)\r\n    {\r\n        \/\/\r\n    }\r\n\r\n    \/**\r\n     * Display the specified resource.\r\n     *\r\n     * @param  \\App\\User  $user\r\n     * @return \\Illuminate\\Http\\Response\r\n     *\/\r\n    public function show(<strong>User $user<\/strong>)\r\n    {\r\n        \/\/\r\n    }\r\n\r\n    \/**\r\n     * Show the form for editing the specified resource.\r\n     *\r\n     * @param  \\App\\User  $user\r\n     * @return \\Illuminate\\Http\\Response\r\n     *\/\r\n    public function edit(<strong>User $user<\/strong>)\r\n    {\r\n        \/\/\r\n    }\r\n\r\n    \/**\r\n     * Update the specified resource in storage.\r\n     *\r\n     * @param  \\Illuminate\\Http\\Request  $request\r\n     * @param  \\App\\User  $user\r\n     * @return \\Illuminate\\Http\\Response\r\n     *\/\r\n    public function update(Request $request, <strong>User $user<\/strong>)\r\n    {\r\n        \/\/\r\n    }\r\n\r\n    \/**\r\n     * Remove the specified resource from storage.\r\n     *\r\n     * @param  \\App\\User  $user\r\n     * @return \\Illuminate\\Http\\Response\r\n     *\/\r\n    public function destroy(<strong>User $user<\/strong>)\r\n    {\r\n        \/\/\r\n    }\r\n}<\/pre>\n<p>\u6ce8\u76ee\u3059\u3079\u304d\u3068\u3053\u308d\u306f\u3001\u3059\u3067\u306b <strong>$user<\/strong> \u304c\u8a72\u5f53\u3059\u308b\u30e6\u30fc\u30b6\u30fc\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306b\u306a\u3063\u3066\u3044\u308b\u3068\u3053\u308d\u3067\u3059\u3002\u901a\u5e38\u306a\u3089ID\u306a\u3069\u304b\u3089\u30e6\u30fc\u30b6\u30fc\u60c5\u5831\u3092\u53d6\u5f97\u3057\u3066\u3001\u305d\u3053\u304b\u3089\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u3068\u3044\u3046\u5177\u5408\u306b\u3072\u3068\u624b\u9593\u3044\u308a\u307e\u3059\u3051\u3069\u3001\u3053\u306e\u8fba\u3082\u3055\u3059\u304c\u306fLaravel\u3067\u3059\u3002<\/p>\n<h2>\uff12\uff0e\u30eb\u30fc\u30c8\u3092\u4f5c\u6210<\/h2>\n<p>\u3044\u3064\u3082\u306e\u3088\u3046\u306b <strong>web.php<\/strong> \u3067\u30a2\u30af\u30bb\u30b9\u3059\u308bURL\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u304c\u3001\u3053\u308c\u3082RESTful API\u306e\u5c02\u7528\u306e\u8a18\u8ff0\u304c\u7528\u610f\u3055\u308c\u3066\u3044\u3066\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3059\u308b\u3060\u3051\u3067\u3059\u3002<\/p>\n<pre><strong>Route::resource('users', 'UserController');<\/strong><\/pre>\n<p>\u3064\u307e\u308a\u3001\u305f\u3063\u305f\u4e00\u884c\u8a18\u8ff0\u3059\u308b\u3060\u3051\u3067\u3001\u4ee5\u4e0b\uff16\u3064\u306e\u30e1\u30bd\u30c3\u30c9\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308b\u308f\u3051\u3067\u3059\u3002<\/p>\n<ul>\n<li><strong>index<\/strong><\/li>\n<li><strong>create<\/strong><\/li>\n<li><strong>store<\/strong><\/li>\n<li><strong>edit<\/strong><\/li>\n<li><strong>update<\/strong><\/li>\n<li><strong>destroy<\/strong><\/li>\n<\/ul>\n<p>URL\u3068\u3057\u3066\u306f\u3001<\/p>\n<pre><strong>(GET) \/users<\/strong>\r\n<strong>(GET) \/users\/create<\/strong>\r\n<strong>(POST) \/users<\/strong>\r\n<strong>(GET) \/users\/{user}<\/strong>\r\n<strong>(GET) \/users\/{user}\/edit<\/strong>\r\n<strong>(PUT) \/users\/{user}<\/strong>\r\n<strong>(DELETE) \/users\/{user}<\/strong><\/pre>\n<p>\u3067\u3059\u3002<\/p>\n<p><em>\u203b\u6ce8\uff1a {user}\u306f\u3001user\u306eID\u304c\u5165\u308a\u307e\u3059\u3002<\/em><\/p>\n<p>\u3055\u3089\u306b\u3001\u5168\u3066\u306e\u30e1\u30bd\u30c3\u30c9\u304c\u5fc5\u8981\u3067\u306f\u306a\u3044\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3059\u308c\u3070\u3001\u5236\u9650\u3092\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\uff08show, store, update \u306e\uff13\u3064\u3060\u3051\u4f7f\u3046\u5834\u5408\uff09<\/p>\n<pre><strong>Route::resource('users', 'UserController', [<\/strong>\r\n<strong>    'only' =&gt; ['show', 'store', 'update']<\/strong>\r\n<strong>]);<\/strong><\/pre>\n<h1>\uff13\uff0e\u5404\u30e1\u30bd\u30c3\u30c9\u306e\u4e2d\u8eab\u3092\u8a18\u8ff0<\/h1>\n<p>\u3055\u3041\u3001\u3042\u3068\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u30ec\u30b3\u30fc\u30c9\u3092\u53d6\u5f97\u3057\u305f\u308a\u3001\u4fdd\u5b58\u3057\u305f\u308a\u3059\u308b\u8a18\u8ff0\u3092\u958b\u767a\u5185\u5bb9\u306b\u6cbf\u3063\u3066\u8ffd\u52a0\u3057\u3066\u3044\u304f\u3060\u3051\u3067OK\u3067\u3059\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u79c1\u306e\u5834\u5408\u3001\u51e6\u7406\u304c\u3046\u307e\u304f\u8a00\u3063\u305f\u304b\u3069\u3046\u304b\u3092 {result: true} \u306e\u3088\u3046\u306a\u5f62\u3067JSON\u3067\u8fd4\u3059\u3053\u3068\u304c\u591a\u3044\u3067\u3059\u3002<\/p>\n<p>\u4eca\u306eLaravel\u306f\u3053\u3053\u3082\u8a18\u8ff0\u304c\u3068\u3066\u3082\u7c21\u5358\u3067\u3001\u5358\u306b\u914d\u5217\u3092\u8fd4\u3059\u3060\u3051\u3067\u81ea\u52d5\u3067JSON\u5316\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<pre><strong>return ['result' =&gt; $result];<\/strong><\/pre>\n<p>Laravel\u306e\u4fbf\u5229\u3055\u306f\u3069\u3053\u307e\u3067\u884c\u304f\u306e\u3067\u3057\u3087\u3046\u304b\uff01\uff1f\uff08\u7b11\uff09<\/p>\n<h1>\u3061\u306a\u307f\u306b<\/h1>\n<p>\u305f\u3068\u3048\u3070\u3001AJAX\u3092\u3064\u304b\u3063\u3066\u30c7\u30fc\u30bf\u3078\u306e\u30a2\u30af\u30bb\u30b9\u3092\u3059\u308b\u5834\u5408\u3001\u305f\u307e\u306b\u4e00\u6c17\u306b\u300c\u524a\u9664\u3057\u305f\u3044\u306a\u30fc\u300d\u306a\u3093\u3066\u601d\u3046\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002\u3044\u308f\u3086\u308b bulk delete \u3068\u3044\u3046\u3084\u3064\u3067\u3059\u306d\u3002<\/p>\n<p>\u305d\u3093\u306a\u5834\u5408\u79c1\u306f\u3001\u30eb\u30fc\u30c8\u306b<\/p>\n<pre><strong>Route::post('destroy_all', 'UserController@destroyAll');<\/strong><\/pre>\n<p>\u3068\u3044\u3046\u5177\u5408\u306b\u4e00\u6c17\u306b\u524a\u9664\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u3092\u8ffd\u52a0\u3057\u3066\u9001\u4fe1\u3055\u308c\u305fID\u304b\u3089\u8a72\u5f53\u3059\u308b\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u3063\u3066\u3001\u3053\u308c\u307e\u3067\u3044\u308d\u3044\u308d\u63a2\u3057\u307e\u3057\u305f\u3051\u3069\u4ed5\u69d8\u3068\u3057\u3066\u304d\u3061\u3063\u3068\u6c7a\u307e\u3063\u3066\u3044\u306a\u3044\u3088\u3046\u306a\u3093\u3067\u3059\u3088\u306d\u3002\u30eb\u30fc\u30c8\u304c\u3072\u3068\u3064\u3060\u3051\u6574\u9813\u3055\u308c\u3066\u306a\u304f\u3066\u3001\u30b9\u30bf\u30a4\u30ea\u30c3\u30b7\u30e5\u3058\u3083\u306a\u3044\u3067\u3059\u3051\u3069\u3001\u3053\u306e\u8fba\u306f\u4ed5\u65b9\u306a\u3044\u306e\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u306d\u3002<\/p>\n<p>\u3068\u3001\u3044\u3046\u3053\u3068\u3067\u4eca\u56de\u306fLaravel 5.6 \u3092\u4f7f\u3063\u305f<strong>RESTful API<\/strong>\u306e\u4f5c\u6210\u65b9\u6cd5\u3067\u3057\u305f\u3002<br \/>\n\u3067\u306f\u3067\u306f\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u73fe\u5728\u306e\u958b\u767a\u3067\u7c21\u5358\u306aRESTful API\u306e\u958b\u767a\u3092\u3057\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001Laravel\u3092\u4f7f\u3046\u3068\u9a5a\u304f\u307b\u3069\u7c21\u5358\u306b\uff08\u4f5c\u696d\u3092\u7701\u7565\u3057\u3066\uff09\u5b9f\u88c5\u3092\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 \u305d\u3053\u3067\u3001\u4eca\u56de &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=338\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;\u305f\u3063\u305f\uff13\u30b9\u30c6\u30c3\u30d7\uff01Laravel 5.6\u3067RESTful API&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":269,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[46],"class_list":["post-338","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","tag-restful"],"_links":{"self":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/338","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=338"}],"version-history":[{"count":2,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/338\/revisions"}],"predecessor-version":[{"id":340,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/338\/revisions\/340"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/269"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}