Pada tutorial laravel sebelumnya kita sudah membuat slug dan soft delete. Tutorial kali ini kita akan membahas cara restore dan delete permanent.
Tutorial Sebelumnya - Boilerplate Laravel 5 : Membuat Slug Dan Soft Delete
Yang pertama kita akan menampilkan data sampah (soft delete) yang kemarin sudah dibahas. Silahkan buat tombol trashed di dalam Post dengan cara buka file index.blade.php di dalam folder resources/views/admin/posts/ setelah itu tambahkan tombol trashed sebelah tombol add, lihat script dibawah.
<a class="btn btn-warning" href="{{ route('post.trashed') }}">Trashed</a>
index.blade.php (posts)
@extends('layouts.app')
@section('content')
<div class="panel panel-default">
<div class="panel-heading">
Posts
</div>
<div class="panel-body">
<div class="text-left">
<a class="btn btn-success" href="{{ route('post.create') }}">Add</a>
<a class="btn btn-warning" href="{{ route('post.trashed') }}">Trashed</a>
</div>
<table class="table table-hover">
<thead>
<th>Image</th>
<th>Title</th>
<th class="text-center">Action</th>
</thead>
<tbody>
@if($posts->count() > 0)
@foreach($posts as $post)
<tr>
<td><img src="{{ url($post->featured) }}" alt="{{ $post->title }}" width="90px" height="50px"></td>
<td>{{ $post->title }}</td>
<td class="text-center">
<a href="{{ route('post.edit', [ 'id'=>$post->id ]) }}" class="btn btn-xs btn-info">
Edit
</a>
<a href="{{ route('post.delete', [ 'id'=>$post->id ]) }}" class="btn btn-xs btn-danger">
Delete
</a>
</td>
</tr>
@endforeach
@else
<tr>
<th colspan='3' class="text-center">Nothing Posts</th>
<tr>
@endif
</tbody>
</table>
</div>
</div>
@stop
Buka terminal kalian dan jalankan aplikasinya
Maka akan ada tombol trashed seperti gambar diatas.
Selanjutnya kita akan membuat tampilan trashed (data sampah). buat file bernama trashed.blade.php di dalam folder resources/views/admin/posts/
dan masukan script dibawah.
trashed.blade.php
@extends('layouts.app')
@section('content')
<div class="panel panel-default">
<div class="panel-heading">
Posts Trashed
</div>
<div class="panel-body">
<div class="text-left">
</div>
<table class="table table-hover">
<thead>
<th>Image</th>
<th>Title</th>
<th class="text-center">Action</th>
</thead>
<tbody>
@if($posts->count() >0)
@foreach($posts as $post)
<tr>
<td><img src="{{ url($post->featured) }}" alt="{{ $post->title }}" width="90px" height="50px"></td>
<td>{{ $post->title }}</td>
<td class="text-center">
<a href="{{ route('post.restore', [ 'id'=>$post->id ]) }}" class="btn btn-xs btn-info">
Restore
</a>
<a href="{{ route('post.destroy', [ 'id'=>$post->id ]) }}" class="btn btn-xs btn-danger">
Destroy
</a>
</td>
</tr>
@endforeach
@else
<tr>
<th colspan='3' class="text-center">Nothing Trashed Posts</th>
<tr>
@endif
</tbody>
</table>
</div>
</div>
@stop
Setelah itu kita akan membuat route, silahkan edit file web.php di dalam folder routes
Route::get('/post/trashed', [
'uses' => 'PostsController@trashed',
'as' => 'post.trashed'
]);
Route::get('/post/destroy/{id}', [
'uses' => 'PostsController@destroy',
'as' => 'post.destroy'
]);
Route::get('/post/restore/{id}', [
'uses' => 'PostsController@restore',
'as' => 'post.restore'
]);
web.php
<?php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::group(['prefix' => 'admin', 'middleware' => 'auth'],function(){
Route::get('/home', [
'uses' => 'HomeController@index',
'as' => 'home'
]);
Route::get('/categories', [
'uses' => 'CategoriesController@index',
'as' => 'categories'
]);
Route::get('/category/create', [
'uses' => 'CategoriesController@create',
'as' => 'category.create'
]);
Route::post('/category/store', [
'uses' => 'CategoriesController@store',
'as' => 'category.store'
]);
Route::get('/category/edit/{id}', [
'uses' => 'CategoriesController@edit',
'as' => 'category.edit'
]);
Route::get('/category/delete/{id}', [
'uses' => 'CategoriesController@destroy',
'as' => 'category.delete'
]);
Route::post('/category/update/{id}', [
'uses' => 'CategoriesController@update',
'as' => 'category.update'
]);
Route::get('/posts', [
'uses' => 'PostsController@index',
'as' => 'posts'
]);
Route::get('/post/create', [
'uses' => 'PostsController@create',
'as' => 'post.create'
]);
Route::post('/post/store', [
'uses' => 'PostsController@store',
'as' => 'post.store'
]);
Route::get('/post/edit/{id}', [
'uses' => 'PostsController@edit',
'as' => 'post.edit'
]);
Route::get('/post/delete/{id}', [
'uses' => 'PostsController@delete',
'as' => 'post.delete'
]);
Route::post('/post/update/{id}', [
'uses' => 'PostsController@update',
'as' => 'post.update'
]);
Route::get('/post/trashed', [
'uses' => 'PostsController@trashed',
'as' => 'post.trashed'
]);
Route::get('/post/destroy/{id}', [
'uses' => 'PostsController@destroy',
'as' => 'post.destroy'
]);
Route::get('/post/restore/{id}', [
'uses' => 'PostsController@restore',
'as' => 'post.restore'
]);
});
yang terakhir kita akan membuat controllernya, buka file PostsController.php didalam folder app/Http/Controllers/ silahkan edit seperti dibawah
PostsController.php
<?php
namespace App\Http\Controllers;
use Session;
use App\Post;
use App\Category;
use Illuminate\Http\Request;
class PostsController extends Controller
{
public function index()
{
return view('admin.posts.index')->with('posts', Post::all());
}
public function create()
{
$categories = Category::all();
if($categories->count() == 0){
Session::flash('info', 'category nothing');
return redirect()->back();
}
return view('admin.posts.create')->with('categories', $categories);
}
public function store(Request $request)
{
$this->validate($request, [
"title" => "required",
"featured" => "required|image",
"content" => "required",
"id_category" => "required"
]);
$featured = $request->featured;
$featured_new_name = time().str_replace(' ','-',$featured->getClientOriginalName());
$featured->move('uploads', $featured_new_name);
$post = Post::create([
"title" => $request->title,
"content" => $request->content,
"featured" => "uploads/" . $featured_new_name,
"id_category" => $request->id_category,
"slug" => str_slug($request->title)
]);
Session::flash('success', 'save success');
return redirect()->route('posts');
}
public function show($id)
{
}
public function edit($id)
{
$post = Post::find($id);
return view('admin.posts.edit')->with('post', $post)->with('categories', Category::all());
}
public function update(Request $request, $id)
{
$this->validate($request, [
"title" => "required",
"content" => "required",
"id_category" => "required"
]);
$post = Post::find($id);
if($request->hasFile('featured')){
File::delete($post->featured);
$featured = $request->featured;
$featured_new_name = time().str_replace(' ','-',$featured->getClientOriginalName());
$featured->move('uploads', $featured_new_name);
$post->featured = "uploads/".$featured_new_name ;
}
$post->title = $request->title;
$post->content = $request->content;
$post->id_category = $request->id_category;
$post->slug = str_slug($request->title);
$post->save();
Session::flash('success', 'update success');
return redirect()->route('posts');
}
public function delete($id)
{
$post = Post::find($id);
//File::delete($post->featured);
$post->delete();
Session::flash('success', 'delete success');
return redirect()->route('posts');
}
public function destroy($id)
{
$post = Post::withTrashed()->where('id', $id)->first();
File::delete($post->featured);
$post->forceDelete();
Session::flash('success', 'destroy success');
return redirect()->back();
}
public function trashed()
{
$posts = Post::onlyTrashed()->get();
return view('admin.posts.trashed')->with('posts', $posts);
}
public function restore($id)
{
$post = Post::withTrashed()->where('id', $id)->first();
$post->restore();
Session::flash('success', 'restore success');
return redirect()->route('posts');
}
}
Coba jalankan aplikasinya, maka akan ada tampilan seperti gambar dibawah.
Pada gambar di atas terdapat tombol restore, ketika di klik maka data akan kembali. sedangkan destroy ketika diklik maka data benar-benar dihapus permanen.
Sekian untuk tutorial laravel kali ini, semoga bermanfaat.
Tutorial Selanjutnya - Boilerplate Laravel 7 : Membuat CRUD Tag