artisan 명령어(모델을 만들면서 마이그레이션 파일도 동시에 만듬.)
php artisan make:model Board --migration
php artisan make:model Board -m주의사항 : Model명은 "단수"입니다. Table명은 "복수"입니다.
Model => 위치 : app/Models/Board.php
Migration => 위치 : database/migrations/2020_12_10_155223_create_boards_table.php
artisan 명령어
php artisan make:controller Admin/Setting/BoardController --resource
Controller => 위치 : app/Http/Controllers/Admin/Setting/BoardController.php
index => 목록, 리스트
컨트롤러 코드
public function index()
{
$lists = Board::latest();
$view = [
'lists' => $lists->paginate(),
];
return view('admin.setting.boards.index', $view);
}
뷰 코드(index.blade.php)
@extends('admin.layouts.app')
@section('content')
@include('admin.layouts.alert')
<div class="text-right">
<a class="btn btn-sm btn-primary" href="{{ route('rhksfl.board-infos.create') }}">
게시판 추가
</a>
</div>
<hr>
<div class="table-responsive">
<table class="table text-center">
<thead class="bg-primary text-white">
<tr>
<th>Seq</th>
<th>Code</th>
<th>Label</th>
<th>UseSecret</th>
<th>ForceSecret</th>
<th>ListLevel</th>
<th>ViewLevel</th>
<th>WriteLevel</th>
<th>Skin</th>
<th></th>
</tr>
</thead>
<tbody>
@forelse($lists as $list)
<tr>
<td>{{ $lists->total() - ($lists->perPage() * ($lists->currentPage() - 1)) - $loop->index }}</td>
<td>{{ $list-> code}}</td>
<td>{{ $list-> label}}</td>
<td>{{ $list-> use_secret}}</td>
<td>{{ $list-> force_secret}}</td>
<td>{{ $list-> list_level}}</td>
<td>{{ $list-> view_level}}</td>
<td>{{ $list-> write_level}}</td>
<td>{{ $list-> skin}}</td>
<td>
<a class="btn btn-sm btn-primary" href="{{ route('rhksfl.board-infos.show', ['board_info' => $list->id]) }}">
자세히 보기
</a>
<a class="btn btn-sm btn-warning" href="{{ route('rhksfl.board-infos.edit', ['board_info' => $list->id]) }}">
수정
</a>
<form class="d-inline" method="post" action="{{ route('rhksfl.board-infos.destroy', ['board_info' => $list->id]) }}">
@csrf
@method('DELETE')
<button class="btn btn-sm btn-danger" type="submit">
삭제
</button>
</form>
</td>
</tr>
@empty
<tr>
<td class="text-center" colspan="15">게시판이 없습니다.</td>
</tr>
@endforelse
</tbody>
</table>
</div>
@endsection
show => 내용, 자세히 보기
컨트롤러 코드
public function show(Board $boardInfo)
{
$view = [
'view' => $boardInfo,
'user_levels' => collect(config('ext.user.user_level.roles')),
];
return view('admin.setting.boards.show', $view);
}
뷰 코드(show.blade.php)
@extends('admin.layouts.app')
@section('content')
<div class="table-responsive">
<table class="table table-bordered">
<tbody>
<tr>
<th class="bg-light" width="200">게시판 코드</th>
<td>{{ $view->code }}</td>
</tr>
<tr>
<th class="bg-light" width="200">게시판 이름</th>
<td>{{ $view->label }}</td>
</tr>
<tr>
<th class="bg-light" width="200">파일 사용 여부</th>
<td>{{ $view->use_file }}</td>
</tr>
<tr>
<th class="bg-light" width="200">최대 첨부 파일 갯수</th>
<td>{{ $view->max_files }}</td>
</tr>
<tr>
<th class="bg-light" width="200">에디터 사용 여부</th>
<td>{{ $view->use_editor }}</td>
</tr>
<tr>
<th class="bg-light" width="200">답글 사용 여부</th>
<td>{{ $view->use_reply }}</td>
</tr>
<tr>
<th class="bg-light" width="200">댓글 사용 여부</th>
<td>{{ $view->use_comment }}</td>
</tr>
<tr>
<th class="bg-light" width="200">댓글 로그인 사용 여부</th>
<td>{{ $view->use_comment_auth }}</td>
</tr>
<tr>
<th class="bg-light" width="200">비밀글 사용 여부</th>
<td>{{ $view->use_secret }}</td>
</tr>
<tr>
<th class="bg-light" width="200">강제 비밀글 사용 여부</th>
<td>{{ $view->force_secret }}</td>
</tr>
<tr>
<th class="bg-light" width="200">목록 보기 레벨</th>
<td>{{ $user_levels->where('level', $view->list_level)->pluck('label')->first() }}</td>
</tr>
<tr>
<th class="bg-light" width="200">글 보기 레벨</th>
<td>{{ $user_levels->where('level', $view->view_level)->pluck('label')->first() }}</td>
</tr>
<tr>
<th class="bg-light" width="200">글 쓰기 레벨</th>
<td>{{ $user_levels->where('level', $view->write_level)->pluck('label')->first() }}</td>
</tr>
<tr>
<th class="bg-light" width="200">게시판 스킨</th>
<td>{{ $view->skin }}</td>
</tr>
</tbody>
</table>
<hr>
<a href="{{ route('rhksfl.board-infos.index') }}" class="btn btn-sm btn-success">
목록
</a>
</div>
@endsection
create => 내용 작성, 추가 화면
컨트롤러 코드
public function create()
{
$form = new Board();
// dd(config('ext.user.user_level.roles'));
$view = [
'form' => $form,
];
return view('admin.setting.boards.create', $view);
}
뷰 코드(create.blade.php)
@extends('admin.layouts.app')
@section('content')
<form method="post" enctype="multipart/form-data" action="{{ route('rhksfl.board-infos.store') }}">
<div class="row justify-content-center">
<div class="col-10">
@include('admin.setting.boards.partial.form')
</div>
</div>
<hr>
<button type="submit" class="btn btn-sm btn-primary">
등록
</button>
<a href="{{ route('rhksfl.board-infos.index') }}" class="btn btn-sm btn-danger">
취소
</a>
</form>
@endsection
뷰 코드 공통으로 쓰이는 부분(form.blade.php)
@csrf
<div class="form-group">
<label for="code">게시판 코드</label>
<input type="text" class="form-control @error('code') is-invalid @enderror" id="code" name="code"
value="{{ old('code', $form->code) }}">
@error('code')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="label">게시판 이름</label>
<input type="text" class="form-control @error('label') is-invalid @enderror" id="label" name="label"
value="{{ old('label', $form->label) }}">
@error('label')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="use_file">파일 사용 여부</label>
<select id="use_file" name="use_file" class="form-control @error('use_file') is-invalid @enderror">
<option value="N" @if(old('use_file', $form->use_file) === 'N') selected @endif>N</option>
<option value="Y" @if(old('use_file', $form->use_file) === 'Y') selected @endif>Y</option>
</select>
@error('use_file')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="max_files">최대 첨부 파일 갯수</label>
<input type="text" class="form-control @error('max_files') is-invalid @enderror" id="max_files" name="max_files"
value="{{ old('max_files', $form->max_files ?? 0) }}">
@error('max_files')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="use_editor">에디터 사용 여부</label>
<select id="use_editor" name="use_editor" class="form-control @error('use_editor') is-invalid @enderror">
<option value="N" @if(old('use_editor', $form->use_editor) === 'N') selected @endif>N</option>
<option value="Y" @if(old('use_editor', $form->use_editor) === 'Y') selected @endif>Y</option>
</select>
@error('use_editor')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="use_reply">답글 사용 여부</label>
<select id="use_reply" name="use_reply" class="form-control @error('use_reply') is-invalid @enderror">
<option value="N" @if(old('use_reply', $form->use_reply) === 'N') selected @endif>N</option>
<option value="Y" @if(old('use_reply', $form->use_reply) === 'Y') selected @endif>Y</option>
</select>
@error('use_reply')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="use_comment">댓글 사용 여부</label>
<select id="use_comment" name="use_comment" class="form-control @error('use_comment') is-invalid @enderror">
<option value="N" @if(old('use_comment', $form->use_comment) === 'N') selected @endif>N</option>
<option value="Y" @if(old('use_comment', $form->use_comment) === 'Y') selected @endif>Y</option>
</select>
@error('use_comment')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="use_comment_auth">댓글 로그인 사용 여부</label>
<select id="use_comment_auth" name="use_comment_auth"
class="form-control @error('use_comment_auth') is-invalid @enderror">
<option value="Y" @if(old('use_comment_auth', $form->use_comment_auth) === 'Y') selected @endif>Y</option>
<option value="N" @if(old('use_comment_auth', $form->use_comment_auth) === 'N') selected @endif>N</option>
</select>
@error('use_comment_auth')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="use_secret">비밀글 사용 여부</label>
<select id="use_secret" name="use_secret" class="form-control @error('use_secret') is-invalid @enderror">
<option value="N" @if(old('use_secret', $form->use_secret) === 'N') selected @endif>N</option>
<option value="Y" @if(old('use_secret', $form->use_secret) === 'Y') selected @endif>Y</option>
</select>
@error('use_secret')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="force_secret">강제 비밀글 사용 여부</label>
<select id="force_secret" name="force_secret" class="form-control @error('force_secret') is-invalid @enderror">
<option value="N" @if(old('force_secret', $form->force_secret) === 'N') selected @endif>N</option>
<option value="Y" @if(old('force_secret', $form->force_secret) === 'Y') selected @endif>Y</option>
</select>
@error('force_secret')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="list_level">목록 보기 레벨</label>
<select id="list_level" name="list_level" class="form-control @error('list_level') is-invalid @enderror">
@foreach(config('ext.user.user_level.roles') as $role)
<option value="{{ $role['level'] }}"
@if(old('list_level', $form->list_level ?? 0) === $role['level']) selected @endif>
{{ $role['label'] }}
</option>
@endforeach
</select>
@error('list_level')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="view_level">글 보기 레벨</label>
<select id="view_level" name="view_level" class="form-control @error('view_level') is-invalid @enderror">
@foreach(config('ext.user.user_level.roles') as $role)
<option value="{{ $role['level'] }}"
@if(old('view_level', $form->view_level ?? 0) === $role['level']) selected @endif>
{{ $role['label'] }}
</option>
@endforeach
</select>
@error('view_level')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="write_level">글 쓰기 레벨</label>
<select id="write_level" name="write_level" class="form-control @error('write_level') is-invalid @enderror">
@foreach(config('ext.user.user_level.roles') as $role)
<option value="{{ $role['level'] }}"
@if(old('write_level', $form->write_level ?? 1024) === $role['level']) selected @endif>
{{ $role['label'] }}
</option>
@endforeach
</select>
@error('write_level')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="skin">게시판 스킨</label>
<input type="text" class="form-control @error('skin') is-invalid @enderror" id="skin" name="skin"
value="{{ old('skin', $form->skin ?? 'basic') }}">
@error('skin')
<div class="invalid-feedback">{{ $message }}</div>
@enderror
</div>
store => 내용 작성, 추가 DB에 INSERT
public function store(BoardStore $request)
{
$board = new Board();
$board->code = $request->post('code');
$board->label = $request->post('label');
$board->use_file = $request->post('use_file');
$board->max_files = $request->post('max_files');
$board->use_editor = $request->post('use_editor');
$board->use_reply = $request->post('use_reply');
$board->use_comment = $request->post('use_comment');
$board->use_comment_auth = $request->post('use_comment_auth');
$board->use_secret = $request->post('use_secret');
$board->force_secret = $request->post('force_secret');
$board->list_level = $request->post('list_level');
$board->view_level = $request->post('view_level');
$board->write_level = $request->post('write_level');
$board->skin = $request->post('skin');
$board->save();
return redirect(route('rhksfl.board-infos.index'))->with('alert-success', '추가되었습니다.');
}
edit => 내용 수정 화면
컨트롤러 코드
public function edit(Board $boardInfo)
{
$view = [
'form' => $boardInfo,
];
return view('admin.setting.boards.edit', $view);
}
뷰 코드(edit.blade.php)
@extends('admin.layouts.app')
@section('content')
<form method="post" enctype="multipart/form-data" action="{{ route('rhksfl.board-infos.update', ['board_info' => $form->id]) }}">
@method('PUT')
<div class="row justify-content-center">
<div class="col-10">
@include('admin.setting.boards.partial.form')
</div>
</div>
<hr>
<button type="submit" class="btn btn-sm btn-warning">
수정
</button>
<a href="{{ route('rhksfl.board-infos.index') }}" class="btn btn-sm btn-danger">
취소
</a>
</form>
@endsection
update => 내용 수정 DB에 UPDATE
컨트롤러 코드
public function update(BoardUpdate $request, Board $boardInfo)
{
$boardInfo->code = $request->post('code');
$boardInfo->label = $request->post('label');
$boardInfo->use_file = $request->post('use_file');
$boardInfo->max_files = $request->post('max_files');
$boardInfo->use_editor = $request->post('use_editor');
$boardInfo->use_reply = $request->post('use_reply');
$boardInfo->use_comment = $request->post('use_comment');
$boardInfo->use_comment_auth = $request->post('use_comment_auth');
$boardInfo->use_secret = $request->post('use_secret');
$boardInfo->force_secret = $request->post('force_secret');
$boardInfo->list_level = $request->post('list_level');
$boardInfo->view_level = $request->post('view_level');
$boardInfo->write_level = $request->post('write_level');
$boardInfo->skin = $request->post('skin');
$boardInfo->save();
return redirect(route('rhksfl.board-infos.index'))->with('alert-success', '수정되었습니다.');
}
delete => 내용 삭제 DB에 DELETE
컨트롤러 코드
public function destroy(Board $boardInfo)
{
$boardInfo->delete();
return redirect(route('rhksfl.board-infos.index'))->with('alert-danger', '삭제되었습니다.');
}
라라벨 공부중입니다 도움이 많이됩니다 감사합니다 ㅎㅎ!