LoginSignup
2
1

More than 1 year has passed since last update.

Laravelで簡単なCMSを作成(記事を更新)

Last updated at Posted at 2021-05-03

Laravelで簡単なCMSを作ってみます。

環境構築から記事の登録までは以下の記事を参考にどうぞ。

今回は記事の更新処理を処理を作ってみます。

Controllerを作成

ArticlesControllerにeditupdateメソッドを追加します。

app/Http/Controllers/ArticlesController.php
<?php

namespace App\Http\Controllers;

use App\Models\Article;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;

class ArticlesController extends Controller
{
    // 記事の登録処理など

    public function edit(Article $article)
    {
        return view('articles.edit', ['article' => $article]);
    }

    public function update(Request $request, Article $article)
    {
        $article->fill($request->all())->save();
        return redirect('/articles');
    }
}

Viewを作成

更新ページを用意します。

resources/views/articles/edit.blade.php
<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Articles') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 bg-white border-b border-gray-200">

                    <form action="/article/update/{{$article->id}}" method="post">
                        @csrf
                        <div class="grid grid-cols-1 gap-6">
                            <label class="block">
                                <span class="text-gray-700">Title</span>
                                <input type="text" name="title" class="mt-1 block w-full" value="{{$article->title}}">
                            </label>
                            <label class="block">
                                <span class="text-gray-700">Body</span>
                                <textarea class="mt-1 block w-full" rows="3" name="body">{{$article->body}}</textarea>
                            </label>
                            <input type="submit" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" value="send">
                        </div>
                    </form>

                </div>
            </div>
        </div>
    </div>
</x-app-layout>

ルートを登録

routes/web.php
<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth'])->name('dashboard');


Route::get('/articles', 'App\Http\Controllers\ArticlesController@index');
Route::get('/article/add', 'App\Http\Controllers\ArticlesController@add');
Route::post('/article/add', 'App\Http\Controllers\ArticlesController@create');

// 更新ページへのルートを登録
Route::get('/article/edit/{article}', 'App\Http\Controllers\ArticlesController@edit');
Route::post('/article/update/{article}', 'App\Http\Controllers\ArticlesController@update');

require __DIR__.'/auth.php';

これで、記事の登録に加えて更新もできるようになりました。記事を更新するためには/article/edit/1のようにarticle_idをURIに含めてリクエストしてください。ルートモデル結合を利用しているので、リクエストされたarticle_idに一致するArticleモデルが生成されサービスコンテナを通じてArticlesControllereditメソッドに自動的に注入されます。

作成したリソースは以下になります。

メソッド URI アクション
GET /articles 一覧
GET /article/add 記事登録ページ表示
POST /article/add 記事登録
GET /article/edit/{article} 記事更新ページ表示
POST /article/edit/{article} 記事登録

作成した記事にタグを付ける機能を実装するには、下記の記事を参考に。

2
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1