12. Community edit
Community 컨트롤러 수정
// 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 활용
// 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"
Author And Source
이 문제에 관하여(12. Community edit), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@yeomc/12-Community-edit저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)