Membuat Blog dengan Laravel 5.6 – Filter Post berdasarkan Author
Pada bagian ketujuh ini kita akan :
- Melakukan filter post berdasarkan author
- Menggunakan gravatar
Filter the Post by Author
Dengan fitur ini, visitor dapat melihat post mana saja oleh penulis tertentu ketika mengklik nama penulis pada post. Pertama tama, kita tambahkan route/web.php :
1 2 3 4 |
Route::get('/author/{author}',[ 'uses' => 'BlogController@author', 'as' => 'author' ]); |
Buka BlogController.php dan tambahkan author() method :
1 2 3 4 5 6 7 8 9 10 11 |
public function author(User $author){ $authorName = $author->name; $posts = $author->posts() ->with('category') ->latestFirst() ->published() ->paginate($this->limit); return view("blog.index", compact('posts','authorName')); } |
Buat kolom slug pada table users :
1 |
php artisan make:migration alter_users_add_slug --table=users |
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 32 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class AlterUsersAddSlug extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('users', function (Blueprint $table) { $table->string('slug'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('users', function (Blueprint $table) { $table->dropColumn('slug'); }); } } |
Jalankan migrasi : php artisan migrate
Tambahkan slug ke UsersTableSeeder.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[ 'name' => "Super Administrator", 'slug' => "super-administrator", 'password' => bcrypt('123secret') ], [ 'name' => "Administrator", 'slug' => "administrator", 'password' => bcrypt('123secret') ], [ 'name' => "Client User", 'slug' => "client-user", 'password' => bcrypt('123secret') ], |
Jalankan seed : php artisan db:seed
Buka User model dan tambahkan getRouteKeyName() method :
1 2 3 |
public function getRouteKeyName(){ return 'slug'; } |
Lalu modifikasi index.blade.php
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
@extends('layouts.main') @section('content') <!-- Page Content --> <div class="container"> <div class="row"> <!-- Blog Entries Column --> <div class="col-md-8"> <h1 class="my-4"> </h1> @if(!$posts->count()) <div class="alert alert-warning"> <p>Nothing Found</p> </div> @else @if(isset($categoryName)) <div class="alert alert-info"> <p>Category: <strong>{{ $categoryName }}</strong></p> </div> @endif @if(isset($authorName)) <div class="alert alert-info"> <p>Author: <strong>{{ $authorName }}</strong></p> </div> @endif @foreach($posts as $post) <!-- Blog Post --> <div class="card mb-4"> @if($post->image_url) <img class="card-img-top" src="{{ $post->image_url }}" alt="{{ $post->slug }}"> @endif <div class="card-body"> <h2 class="card-title"><a href="{{ route('blog.show', $post->slug) }}">{{ $post->title }}</a></h2> <p class="card-text">{!! $post->excerpt_html !!}</p> <a href="{{ route('blog.show', $post->slug) }}" class="btn btn-primary">Read More →</a> </div> <div class="card-footer text-muted"> Posted on {{ $post->date }} by <a href="{{ route('author', $post->author->slug) }}">{{ $post->author->name }}</a> in category <a href="{{ route('category', $post->category->slug )}}">{{ $post->category->title }}</a> </div> </div> @endforeach @endif <!-- Pagination --> <ul class="pagination justify-content-center mb-4"> {{ $posts->links() }} </ul> </div> @include('layouts.sidebar') </div> <!-- /.row --> </div> <!-- /.container --> @endsection |
Juga modifikasi show.blade.php :
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
@extends('layouts.main') @section('content') <!-- Page Content --> <div class="container"> <div class="row"> <!-- Post Content Column --> <div class="col-lg-8"> <!-- Title --> <h1 class="mt-4">{{ $post->title }}</h1> <!-- Author --> <p class="lead"> by <a href="{{ route('author',$post->author->slug) }}">{{ $post->author->name }}</a> </p> <hr> <!-- Date/Time --> <p>Posted {{ $post->date }} on <strong><a href="{{ route('category',$post->category->slug)}}">{{ $post->category->title}}</a></strong></p> <hr> @if($post->image_url) <!-- Preview Image --> <img class="img-fluid rounded" src="http://placehold.it/900x300" alt=""> <hr> @endif <!-- Post Content --> {!! $post->body_html !!} <hr> </div> @include('layouts.sidebar') </div> <!-- /.row --> </div> <!-- /.container --> @endsection |
Selanjutnya, tambahkan kolom bio ke tabel users :
1 |
php artisan make:migration alter_users_add_bio --table=users |
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 32 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class AlterUsersAddBio extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('users', function (Blueprint $table) { $table->text('bio'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('users', function (Blueprint $table) { $table->dropColumn('bio'); }); } } |
Jalankan migrasi : php artisan migrate.
Tambahkan bio menggunakan UsersTableSeeder.php ,jangan lupa untuk menambahkan namespace use Faker\Factory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
[ 'name' => "Super Administrator", 'slug' => "super-administrator", 'password' => bcrypt('123secret'), 'bio' => $faker->text(rand(250,300)) ], [ 'name' => "Administrator", 'slug' => "administrator", 'password' => bcrypt('123secret'), 'bio' => $faker->text(rand(250,300)) ], [ 'name' => "Client User", 'slug' => "client-user", 'password' => bcrypt('123secret'), 'bio' => $faker->text(rand(250,300)) ], |
Jalankan seed : php artisan db:seed
Buka User.php dan tambahkan gravatar() dan bioHtml method :
1 2 3 4 5 6 7 8 9 10 11 |
public function getBioHtmlAttribute($value){ return $this->bio ? Markdown::convertToHtml(e($this->bio)) : NULL; } public function gravatar(){ $email = $this->email; $default = "https://cdn2.iconfinder.com/data/icons/ios-7-icons/50/user_male2-512.png"; $size = 100; return "https://www.gravatar.com/avatar/" . md5( strtolower( trim( $email ) ) ) . "?d=" . urlencode( $default ) . "&s=" . $size; } |
Jangan lupa copy markdown namespace : use GrahamCampbell\Markdown\Facades\Markdown;
Selanjutnya, kita akan menggunakan gravatar untuk menambahkan foto pada bio author. Buka gravatar.com dan register email. Modifikasi show.blade.php :
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
@extends('layouts.main') @section('content') <!-- Page Content --> <div class="container"> <div class="row"> <!-- Post Content Column --> <div class="col-lg-8"> <!-- Title --> <h1 class="mt-4">{{ $post->title }}</h1> <!-- Author --> <p class="lead"> by <a href="{{ route('author',$post->author->slug) }}">{{ $post->author->name }}</a> </p> <hr> <!-- Date/Time --> <p>Posted {{ $post->date }} on <strong><a href="{{ route('category',$post->category->slug)}}">{{ $post->category->title}}</a></strong></p> <hr> @if($post->image_url) <!-- Preview Image --> <img class="img-fluid rounded" src="http://placehold.it/900x300" alt=""> <hr> @endif <!-- Post Content --> <p class="lead">{!! $post->body_html !!}</p> <hr> <article class="post-author padding-10"> <div class="media"> <div class="media-center"> <a href="{{ route('author', $post->author->slug) }}"> <img src="{{ $post->author->gravatar() }}" width="100" height="100" alt="{{ $post->author->name }}" class="media-object"> </a> </div> <div class="media-body" style="padding-left: 10px;"> <h4 class="media-heading"><a href="{{ route('author', $post->author->slug) }}">{{ $post->author->name }}</a></h4> <div class="post-author-count"> <a href="{{ $post->author->slug }}"> <i class="fa fa-clone"></i> <?php $postCount = $post->author->posts()->published()->count();?> {{ $postCount }} {{ str_plural('post', $postCount) }} </a> </div> {!! $post->author->bio_html !!} </div> </div> </article> <br> </div> @include('layouts.sidebar') </div> <!-- /.row --> </div> <!-- /.container --> @endsection |
Kini buka project, apabila ada error, silahkan berikan komentar ya.
Github Commit.
mungkin teman2 nnti ada yang error, ini saya dapat error
1. call undefined app:post kalau gak salah
saya tambahin
//
public function posts(){
return $this->hasMany(Post::class,’author_id’);
}
// di User model
2. Call to undefined function str_plurar()
caranya :
composer require laravel/helpers
semoga membantu
Terima kasih yaaa.. Bisa membantu teman lain yang mengalami error yang sama..