12. Community edit

Community 컨트롤러 수정

laravel 지연 eager 로딩

// CommunityController.php
public function edit(Community $community)
{
  if ($community->user_id != auth()->id()) {
    abort(403);
  }
  $topics = Topic::all();
  $community->load('topics');

  return view('communities.edit', compact('topics', 'community'));
}

edit view 생성 (create 복사 후 수정)

// communities/edit.blade.php
@extends('layouts.app')

@section('content')
    <section class="section hero is-fullheight-with-navbar">
        <div class="hero-body">
            <div class="container">
                <div class="columns is-centered">
                    <div class="column is-8">
                        @component('components.card')
                            @slot('title')
                                {{ __('Edit Community') }}
                            @endslot

                            <form method="POST" action="{{ route('communities.update', $community) }}">
                                @csrf
                                @method('PUT')

                                <div class="field">
                                    <label class="label" for="name">{{ __('Name') }}</label>
                                    <div class="control">
                                        <input id="name" type="text" class="input @error('name') is-danger @enderror"
                                            name="name" value="{{ $community->name }}" required autocomplete="name" autofocus>
                                    </div>

                                    @error('name')
                                        <p class="help is-danger" role="alert">
                                            {{ $message }}
                                        </p>
                                    @enderror
                                </div>

                                <div class="field">
                                    <label class="label" for="topics">{{ __('Topics') }}</label>
                                    <div class="control">
                                        <div class="select is-multiple">
                                            <select id="topics" class="multi-select @error('topics') is-danger @enderror"
                                                name="topics[]" multiple>
                                                @foreach ($topics as $topic)
                                                    <option value="{{ $topic->id }}" @if ($community->topics->contains($topic->id)) selected @endif>
                                                        {{ $topic->name }}
                                                    </option>
                                                @endforeach
                                            </select>
                                        </div>
                                    </div>

                                    @error('topics')
                                        <p class="help is-danger" role="alert">
                                            {{ $message }}
                                        </p>
                                    @enderror
                                </div>

                                <div class="field">
                                    <label class="label" for="description">{{ __('Description') }}</label>
                                    <div class="control">
                                        <textarea id="description" name="description"
                                            class="textarea @error('description') is-danger @enderror">{{ $community->description }}</textarea>
                                    </div>

                                    @error('description')
                                        <p class="help is-danger" role="alert">
                                            {{ $message }}
                                        </p>
                                    @enderror
                                </div>

                                <hr>

                                <div class="field is-grouped is-grouped-right">
                                    <p class="control">
                                        <a class="button is-rounded" href="{{ route('communities.index') }}">
                                            <strong>{{ __('Cancel') }}</strong>
                                        </a>
                                    </p>
                                    <p class="control">
                                        <button type="submit" class="button is-info is-rounded">
                                            <strong>{{ __('Update Community') }}</strong>
                                        </button>
                                    </p>
                                </div>
                            </form>
                        @endcomponent
                    </div>
                </div>
            </div>
        </div>
    </section>
@endsection

Authorization Gate 활용

laravel authorization#gates

// AuthServiceProvider.php
// update gete 등록
public function boot()
{
  $this->registerPolicies();

  Gate::define('update', function ($user, $model) {
    return $user->id && in_array($user->id, [$model->owner_id]);
  });
}

Community 컨트롤러 수정

// CommunityController.php
public function edit(Community $community)
{
  $this->authorize('update', $community);
  
  $topics = Topic::all();
  $community->load('topics');

  return view('communities.edit', compact('topics', 'community'));
}

git commit

git add .
git commit -m "feat: create community edit view"

좋은 웹페이지 즐겨찾기