Pada tutorial laravel sebelumnya kita sudah membuat restore dan delete permanent, Tutorial kali ini kita akan membuat CRUD Tag
Tutorial Sebelumnya - Boilerplate Laravel 6 : Restore Dan Delete Permanent
Pertama kita akan membuat database dengan cara membuat model tag bersamaan dengan migrasi
Membuat model tag
Buka cmd dan masuk ke folder project kalian, ketikan code di bawah
C:\xampp\htdocs\blog>php artisan make:model Tag -m
Maka otomatis akan membuat file model dan migrasi, silahkan buka file migrasi yg tdi di buat di dalam folder database/migration dan tambahkan field tag, lihat script dibawah
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTagsTable extends Migration
{
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->increments('id');
$table->string('tag');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('tags');
}
}
Selanjutnya silahkan edit file Post.php dan Tag.php di dalam folder app yang tadi sudah di buat. lihat script dibawah
Tag.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
protected $fillable= ['tag'];
public function posts(){
return $this->belongsToMany('App\Post');
}
}
Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model
{
use SoftDeletes;
protected $fillable = [
"title", "content", "featured", "id_category", "slug"
];
protected $dates = ['deleted_at'];
public function category(){
return $this->belongsTo('App\Category');
}
public function tags(){
return $this->belongsToMany('App\Tag');
}
}
Setelah beres di edit lanjut ke pembuatan database post_tag, buat file migrasinya
C:\xampp\htdocs\blog> php artisan make:migration create_post_tag_table
dan masukan script dibawah ini
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostTagTable extends Migration
{
public function up()
{
Schema::create('post_tag', function (Blueprint $table) {
$table->increments('id');
$table->integer('post_id');
$table->integer('tag_id');
$table->timestamps();
});
}
public function down()
{
Schema::drop('post_tag');
}
}
Setelah membuat file migrasi tag dan post_tag, selanjutnya kita eksekusi. lihat code dibawah
C:\xampp\htdocs\blog> php artisan migrate
maka akan ada tabel baru bernama tags dan post_tag
Membuat Controller
Selanjunya kita akan membuat controller, buka cmd kalian dan tulis code dibawah
C:\xampp\htdocs\blog> php artisan make:controller TagsController --resource
Maka otomatis TagsController.php terbuat beserta fungsinya di dalam folder app/Http/Controllers, silahkan edit seperti dibawah ini.
TagsController.php
<?php
namespace App\Http\Controllers;
use Session;
use App\Tag;
use Illuminate\Http\Request;
class TagsController extends Controller
{
public function index()
{
return view('admin.tags.index')->with('tags', Tag::all());
}
public function create()
{
return view('admin.tags.create');
}
public function store(Request $request)
{
$this->validate($request, [
'tag' => 'required'
]);
Tag::create([
'tag' => $request->tag
]);
Session::flash('success','save success');
return redirect()->route('tags');
}
public function show($id)
{
//
}
public function edit($id)
{
$tag = Tag::find($id);
return view('admin.tags.edit')->with('tag', $tag);
}
public function update(Request $request, $id)
{
$this->validate($request, [
'tag' => 'required'
]);
$tag = Tag::find($id);
$tag->tag = $request->tag;
$tag->save();
Session::flash('success','update success');
return redirect()->route('tags');
}
public function destroy($id)
{
Tag::destroy($id);
Session::flash('success','delete success');
return redirect()->back();
}
}
Membuat View
Sebelumnya kita buat dlu menu Tag di file app.blade.php di dalam folder resources/views/layouts, silahkan edit seperti dibawah ini
<div class="container">
<div class="row">
@if(Auth::check())
<div class="col-lg-4">
<ul class="list-group">
<li class="list-group-item">
<a href="{{ route('categories') }}">Category</a>
</li>
<li class="list-group-item">
<a href="{{ route('posts') }}">Post</a>
</li>
<li class="list-group-item">
<a href="{{ route('tags') }}">Tag</a>
</li>
</ul>
</div>
@endif
<div class="col-lg-8">
@yield('content')
</div>
</div>
</div>
Setelah itu buat folder tags di dalam resources/views/admin
C:\xampp\htdocs\blog\resources\views\admin> mkdir tags
Buat file index.blade.php di dalam resources/views/admin/tags
C:\xampp\htdocs\blog\resources\views\admin\tags> mkdir index.blade.php
index.blade.php
@extends('layouts.app')
@section('content')
<div class="panel panel-default">
<div class="panel-heading">
Tags
</div>
<div class="panel-body">
<div class="text-left">
<a class="btn btn-success" href="{{ route('tag.create') }}">Add</a>
</div>
<table class="table table-hover">
<thead>
<th>Tags</th>
<th class="text-center">Action</th>
</thead>
<tbody>
@if($tags->count() > 0)
@foreach($tags as $tag)
<tr>
<td>{{ $tag->tag }}</td>
<td class="text-center">
<a href="{{ route('tag.edit', [ 'id'=>$tag->id ]) }}" class="btn btn-xs btn-info">
Edit
</a>
<a href="{{ route('tag.delete', [ 'id'=>$tag->id ]) }}" class="btn btn-xs btn-danger">
Delete
</a>
</td>
</tr>
@endforeach
@else
<tr>
<th colspan='2' class='text-center'>Nothing Tags</th>
</tr>
@endif
</tbody>
</table>
</div>
</div>
@stop
Buat file create.blade.php di dalam resources/views/admin/tags
C:\xampp\htdocs\blog\resources\views\admin\tags> mkdir create.blade.php
create.blade.php
@extends('layouts.app')
@section('content')
@include('admin.includes.errors ')
<div class="panel panel-default">
<div class="panel-heading">
Creat a new tag
</div>
<div class="panel-body">
<form action="{{ route('tag.store') }}" method="post">
{{ csrf_field() }}
<div class="form-group">
<label for="name">Tag</label>
<input type="text" class="form-control" name="tag">
</div>
<div class="form-group">
<div class="text-center">
<button class="btn btn-success" type="submit">Save</button>
</div>
</div>
</form>
</div>
</div>
@stop
Buat file edit.blade.php di dalam resources/views/admin/tags
C:\xampp\htdocs\blog\resources\views\admin\tags> mkdir create.blade.php
edit.blade.php
@extends('layouts.app')
@section('content')
@include('admin.includes.errors ')
<div class="panel panel-default">
<div class="panel-heading">
Edit tag {{ $tag->tag }}
</div>
<div class="panel-body">
<form action="{{ route('tag.update', ['id'=>$tag->id]) }}" method="post">
{{ csrf_field() }}
<div class="form-group">
<label for="name">Name</label>
<input type="text" value="{{ $tag->tag }}" class="form-control" name="tag">
</div>
<div class="form-group">
<div class="text-center">
<button class="btn btn-success" type="submit">Update</button>
</div>
</div>
</form>
</div>
</div>
@stop
Membuat Route
Selanjutnya kita akan membuat route untuk tag, silahkan edit file web.php di dalam folder routes
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'
]);
Route::get('/tags', [
'uses' => 'TagsController@index',
'as' => 'tags'
]);
Route::get('/tag/create', [
'uses' => 'TagsController@create',
'as' => 'tag.create'
]);
Route::post('/tag/store', [
'uses' => 'TagsController@store',
'as' => 'tag.store'
]);
Route::get('/tag/edit/{id}', [
'uses' => 'TagsController@edit',
'as' => 'tag.edit'
]);
Route::get('/tag/delete/{id}', [
'uses' => 'TagsController@destroy',
'as' => 'tag.delete'
]);
Route::post('/tag/update/{id}', [
'uses' => 'TagsController@update',
'as' => 'tag.update'
]);
});
Pada tahap ini kita sudah membuat CRUD tag, Jika berhasil maka akan ada tampilan seperti berikut
Sekian untuk Tutorial Boilerplate Laravel 7 : Membuat CRUD Tag Semoga bermanfaat