반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- 보정명령
- Python
- 소액임금체불
- win32
- auth
- php
- Bootstrap
- Java
- 홈택스
- 사업자계좌
- 전자소송
- elasticSearch
- 체당금
- Sentinel
- 이더리움
- blockchain
- 당사자표시정정신청서
- Blade
- 개인사업자
- cartalyst
- 코로나19
- 코로나
- Laravel
- reactnative
- javascript
- Eclipse
- vue
- Tutorial
- as후기
- 인민공원
Archives
- Today
- Total
그냥 사는 이야기
Laravel 5.3 Blog System - Admin Dashboard - Edit, Delete 본문
반응형
Edit & Delete
resources/views/admin/posts/edit.blade.php
@extends('layouts.dashbord')
@section('content')
<h2>Update Post</h2>
<script type="text/javascript" src="//cdn.tinymce.com/4/tinymce.min.js"></script>
<script type="text/javascript">
tinymce.init({
selector : "textarea",
plugins : ["advlist autolink lists link image charmap print preview anchor", "searchreplace visualblocks code fullscreen", "insertdatetime media table contextmenu paste"],
toolbar : "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
});
</script>
<form action="{{ url('/admin/posts/updatepost') }}" method="post" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="post_id" value="{{ $post->id }}{{ old('post_id') }}">
<input type="hidden" name="post_descriptions" value="{{ $post->id }}{{ old('post_id') }}">
<div class="form-group">
<p>Title</p>
<input type="text" name="title" value="@if(!old('title')){{$post->title}}@endif{{ old('title') }}" required="required" placeholder="Enter title here" class="form-control">
<br>
<p>Description</p>
<input type="text" name="description" value="@if(!old('description')){{$post->description}}@endif{{ old('description') }}" required="required" placeholder="Enter description here" class="form-control">
<br>
<!-- thumbnail upload -->
<p>Thumbnail</p>
<img src="{{ url('img/'.$post->images) }}" id ="showimages" style="max-width:200px;max-height:200px;float:left;"/>
<div class="row">
<div class="col-md-12">
<input type="file" id="inputimages" name="images">
</div>
</div>
</div>
<div class="form-group">
<textarea name='body'class="form-control" rows="20">
@if(!old('body'))
{!! $post->body !!}
@endif
{!! old('body') !!}
</textarea>
</div>
@if($post->active == '1')
<input type="submit" name='publish' class="btn btn-success" value = "Update"/>
@else
<input type="submit" name='publish' class="btn btn-success" value = "Publish"/>
@endif
<input type="submit" name='save' class="btn btn-default" value = "Save As Draft" />
<a href="{{ url('admin/posts/deletepost/'.$post->id.'?_token='.csrf_token()) }}" onclick="return confirm('Are you sure to delete this data');" class="btn btn-danger">Delete</a>
</form>
@endsection
App/Http/Controllers/Auth/PostController
public function edit(Request $request, $slug)
{
public function edit(Request $request, $slug)
{
$post = Post::where('slug', $slug)->first();
if ($post && ($request->user()->id == $post->author_id || $request->user()->is_admin()))
return view('admin/posts/edit')->with('post', $post);
return redirect('/')->withErrors('you have not sufficient permissions');
}
}
public function update(Request $request)
{
$post_id = $request->input('post_id');
$post = Post::find($post_id);
if ($post && ($post->author_id == $request->user()->id || $request->user()->is_admin())) {
$title = $request->input('title');
$slug = str_slug($title);
$duplicate = Post::where('slug', $slug)->first();
if ($duplicate) {
if($duplicate->id != $post_id) {
return redirect('admin/posts/editpost/'.$post->slug)->withErrors('Title already exists.')->withInput();
}
else {
$post->slug = $slug;
}
}
$post->title = $title;
// thumbnail upload
if ($request->file('images')) {
$fileName = str_random(30);
$request->file('images')->move("img/",$fileName);
} else {
$fileName = $post->images;
}
$post->images = $fileName;
$post->body = $request->input('body');
if ($request->has('save')) {
$post->active = 0;
$message = 'Post saved successfully';
$goto = 'admin/posts/editpost/'.$post->slug;
}
else {
$post->active = 1;
$message = 'Post updated successfully';
$goto = 'admin/posts/allposts';
}
$post->save();
return redirect($goto)->withMessage($message);
}
else {
return redirect('/')->withErrors('you have not sufficient permissions');
}
}
public function destroy(Request $request, $id)
{
$post = Post::find($id);
if ($post && ($post->author_id == $request->user()->id || $request->user()->is_admin())) {
$post->delete();
$data['message'] = 'Post deleted Successfully';
}
else {
$data['errors'] = 'Invalid Operation. You have not sufficient permissions';
}
return redirect('admin/posts/allposts')->with($data);
}
routes/web.php
Route::group(['middleware' => ['auth']], function() {
...
// edit form
Route::get('admin/posts/editpost/{slug}','Auth\PostController@edit');
// update data
Route::post('admin/posts/updatepost','Auth\PostController@update');
// delete post
Route::get('admin/posts/deletepost/{id}','Auth\PostController@destroy');
Route::resource('admin/posts', 'Auth\PostController');
});
'Development > Web' 카테고리의 다른 글
Vue에서 error: Unexpected console statement (no-console) (0) | 2020.12.15 |
---|---|
vuejs, vuetify2 에서 polyfill 적용하려면 (0) | 2020.12.11 |
Laravel 5.3 Blog System - Admin Dashboard - Create, Store (0) | 2020.01.23 |
Laravel 5.3 Blog System - Admin Dashboard - All Posts (0) | 2020.01.23 |
Laravel 5.3 Blog System - Admin Dashboard (0) | 2020.01.23 |
Comments