Skip to content

Commit

Permalink
Post Categories and Markdown - Wordpress-Like Blog Laravel 5.7 and Ad…
Browse files Browse the repository at this point in the history
…minLTE 3 (6)
  • Loading branch information
andriindocoder committed Sep 13, 2018
1 parent c7d58c6 commit 024a00e
Show file tree
Hide file tree
Showing 16 changed files with 388 additions and 30 deletions.
16 changes: 16 additions & 0 deletions app/Category.php
@@ -0,0 +1,16 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
public function posts(){
return $this->hasMany(Post::class);
}

public function getRouteKeyName(){
return 'slug';
}
}
13 changes: 13 additions & 0 deletions app/Http/Controllers/BlogController.php
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Request;
use App\Post;
use App\Category;

class BlogController extends Controller
{
Expand All @@ -20,6 +21,18 @@ public function index(){
return view("blog.index", compact('posts'));
}

public function category(Category $category){
$categoryName = $category->title;

$posts = $category->posts()
->with('author')
->latestFirst()
->published()
->paginate($this->limit);

return view("blog.index", compact('posts','categoryName'));
}

public function show(Post $post){

return view("blog.show", compact('post'));
Expand Down
15 changes: 15 additions & 0 deletions app/Post.php
Expand Up @@ -4,11 +4,14 @@

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use GrahamCampbell\Markdown\Facades\Markdown;

class Post extends Model
{
protected $dates = ['published_at'];

protected $fillable = ['category_id'];

public function getImageUrlAttribute($value){

$imageUrl = "";
Expand All @@ -25,10 +28,22 @@ public function author(){
return $this->belongsTo(User::class);
}

public function category(){
return $this->belongsTo(Category::class);
}

public function getDateAttribute($value){
return is_null($this->published_at) ? '' : $this->published_at->diffForHumans();
}

public function getBodyHtmlAttribute($value){
return $this->body ? Markdown::convertToHtml(e($this->body)) : NULL ;
}

public function getExcerptHtmlAttribute($value){
return $this->excerpt ? Markdown::convertToHtml(e($this->excerpt)) : NULL ;
}

public function scopeLatestFirst($query){
return $query->orderBy('created_at', 'desc');
}
Expand Down
35 changes: 35 additions & 0 deletions app/Providers/ComposerServiceProvider.php
@@ -0,0 +1,35 @@
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Category;

class ComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
view()->composer('layouts.sidebar', function($view){
$categories = Category::with(['posts' => function($query){
$query->published();
}])->orderBy('title','asc')->get();

return $view->with('categories',$categories);
});
}

/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
}
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -7,6 +7,7 @@
"require": {
"php": "^7.1.3",
"fideloper/proxy": "^4.0",
"graham-campbell/markdown": "^10.1",
"laravel/framework": "5.6.*",
"laravel/tinker": "^1.0"
},
Expand Down
139 changes: 138 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion config/app.php
Expand Up @@ -150,6 +150,7 @@
/*
* Package Service Providers...
*/
GrahamCampbell\Markdown\MarkdownServiceProvider::class,

/*
* Application Service Providers...
Expand All @@ -159,6 +160,7 @@
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\ComposerServiceProvider::class,

],

Expand Down Expand Up @@ -208,7 +210,7 @@
'URL' => Illuminate\Support\Facades\URL::class,
'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class,

'Markdown' => GrahamCampbell\Markdown\Facades\Markdown::class,
],

];
2 changes: 1 addition & 1 deletion config/database.php
Expand Up @@ -50,7 +50,7 @@
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'strict' => false,
'engine' => null,
],

Expand Down
33 changes: 33 additions & 0 deletions database/migrations/2018_09_12_235448_create_categories.php
@@ -0,0 +1,33 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCategories extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('slug')->unique();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}
@@ -0,0 +1,34 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AlterPostsAddCategoryId extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('restrict');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('posts', function (Blueprint $table) {
$table->dropForeign(['category_id']);
$table->dropColumn('category_id');
});
}
}

0 comments on commit 024a00e

Please sign in to comment.