WordPress-Like Blog Laravel 5.7 and AdminLTE 3 (7) – Filter Post by Author
In this seventh part, we will :
- Filter the post based on author
- Utilize Gravater
Filter the Post by Author
With this feature, the visitor will be able to see the post written by certain author when they click the author name on the post. First, we add route/web.php :
1 2 3 4 |
Route::get('/author/{author}',[ 'uses' => 'BlogController@author', 'as' => 'author' ]); |
Open BlogController.php and add 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')); } |
Add slug column to users table :
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'); }); } } |
Run the migration : php artisan migrate
Add slug to 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') ], |
Run the seed : php artisan db:seed
Open User model and add getRouteKeyName() method :
1 2 3 |
public function getRouteKeyName(){ return 'slug'; } |
Modify 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 |
Also modify 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 |
Next, add bio column to users table :
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'); }); } } |
Run the migration : php artisan migrate.
Add bio using UsersTableSeeder.php , don’t forget to add the 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)) ], |
Run the seed : php artisan db:seed
Open User.php and add gravatar() and 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; } |
Don’t forget to copy markdown namespace : use GrahamCampbell\Markdown\Facades\Markdown;
Afterwards, we will use gravatar to add picture to author bio. Open gravatar.com and register your email. Modify 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 |
Open your project, send me a comment if you encounter any error.
Github Commit.