Skip to content

Commit

Permalink
Create a New Post - Wordpress-Like Blog Laravel 5.7 and AdminLTE 3 (11)
Browse files Browse the repository at this point in the history
  • Loading branch information
andriindocoder committed Sep 14, 2018
1 parent 9ab7003 commit e6f8e75
Show file tree
Hide file tree
Showing 11 changed files with 553 additions and 33 deletions.
53 changes: 49 additions & 4 deletions app/Http/Controllers/Backend/BlogController.php
Expand Up @@ -4,10 +4,20 @@

use Illuminate\Http\Request;
use App\Post;
use App\Http\Requests;
use Intervention\Image\Facades\Image;

class BlogController extends BackendController
{
protected $limit=5;
protected $uploadPath;

public function __construct()
{
parent::__construct();
$this->uploadPath = public_path(config('cms.image.directory'));
}

/**
* Display a listing of the resource.
*
Expand All @@ -26,9 +36,9 @@ public function index()
*
* @return \Illuminate\Http\Response
*/
public function create()
public function create(Post $post)
{
dd("create new blog");
return view("backend.blog.create", compact('post'));
}

/**
Expand All @@ -37,9 +47,44 @@ public function create()
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
public function store(Requests\PostRequest $request)
{
//
$data = $this->handleRequest($request);

$request->user()->posts()->create($data);

return redirect(route('backend.blog.index'))->with('message', 'Post has been added');
}

private function handleRequest($request){
$data = $request->all();

if($request->hasFile('image')){
$image = $request->file('image');
$thumbnail =
$fileName = $image->getClientOriginalName();

$destination = $this->uploadPath;

$uploaded = $image->move($destination, $fileName);

if($uploaded){
$width = config('cms.image.thumbnail.width');
$height = config('cms.image.thumbnail.height');

$extension = $image->getClientOriginalExtension();
$thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $fileName);

Image::make($destination . '/' . $fileName)
->resize($width,$height)
->save($destination . '/' . $thumbnail);
}


$data['image'] = $fileName;
}

return $data;
}

/**
Expand Down
35 changes: 35 additions & 0 deletions app/Http/Requests/PostRequest.php
@@ -0,0 +1,35 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class PostRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'required',
'slug' => 'required|unique:posts',
'body' => 'required',
'published_at' => 'date_format:Y-m-d H:i:s',
'category_id' => 'required',
'image' => 'mimes:jpg,jpeg,bmp,png',
];
}
}
64 changes: 37 additions & 27 deletions app/Post.php
Expand Up @@ -10,33 +10,39 @@ class Post extends Model
{
protected $dates = ['published_at'];

public function getImageUrlAttribute($value){

$imageUrl = "";
if(!is_null($this->image)){
$imageUrl = $this->image;
}else{
$imageUrl = "";
}

return $imageUrl;
}

public function getImageThumbUrlAttribute($value){

$imageUrl = "";
if(!is_null($this->image)){
$ext = substr(strrchr($this->image, '.'), 1);
$thumbnail = str_replace(".{$ext}", "_thumb.{$ext}", $this->image);
$imagePath = public_path() . "/img/" . $thumbnail;
if(file_exists($imagePath)) $imageUrl = asset("img/" . $thumbnail);
$imageUrl = $this->image;
}else{
$imageUrl = "";
}

return $imageUrl;
}
protected $fillable = ['title','slug','excerpt','body','published_at','category_id','image'];

public function getImageUrlAttribute($value){

$imageUrl = "";

if(!is_null($this->image)){
$directory = config('cms.image.directory');
$imagePath = public_path(). "/{$directory}/". $this->image;

if(file_exists($imagePath)) $imageUrl = asset("{$directory}/" .$this->image);
}

return $imageUrl;
}

public function getImageThumbUrlAttribute($value){

$imageUrl = "";

if(!is_null($this->image)){
$directory = config('cms.image.directory');
$ext = substr(strrchr($this->image, '.'), 1);
$thumbnail = str_replace(".{$ext}", "_thumb.{$ext}", $this->image);
$imagePath = public_path() . "/{$directory}/" . $thumbnail;
if(file_exists($imagePath)) $imageUrl = asset("{$directory}/" . $thumbnail);
$imageUrl = $this->image;
}else{
$imageUrl = "";
}

return $imageUrl;
}

public function author(){
return $this->belongsTo(User::class);
Expand All @@ -50,6 +56,10 @@ public function getDateAttribute($value){
return is_null($this->published_at) ? '' : $this->published_at->diffForHumans();
}

public function setPublishedAtAttribute($value){
$this->attributes['published_at'] = $value ? : NULL;
}

public function getBodyHtmlAttribute($value){
return $this->body ? Markdown::convertToHtml(e($this->body)) : NULL ;
}
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Expand Up @@ -8,8 +8,10 @@
"php": "^7.1.3",
"fideloper/proxy": "^4.0",
"graham-campbell/markdown": "^10.1",
"intervention/image": "^2.4",
"laravel/framework": "5.7.*",
"laravel/tinker": "^1.0"
"laravel/tinker": "^1.0",
"laravelcollective/html": "^5.7"
},
"require-dev": {
"beyondcode/laravel-dump-server": "^1.0",
Expand Down

0 comments on commit e6f8e75

Please sign in to comment.