app/Http/Controllers/Backend/PostsController.php
<?php
namespace App\Http\Controllers\Backend;
use App\Http\Controllers\Controller;
use App\Http\Requests\PostRequest;
use App\Repositories\PostRepository;
use App\Repositories\TagRepository;
use App\Tag;
use Illuminate\Support\Facades\Auth;
use Intervention\Image\Facades\Image;
class PostsController extends Controller
{
/**
* @var \App\Repositories\PostRepository
*/
protected $posts;
/**
* @var \App\Repositories\TagRepository
*/
protected $tags;
public function __construct(PostRepository $posts, TagRepository $tags)
{
$this->posts = $posts;
$this->tags = $tags;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\View\View
*/
public function index()
{
$posts = $this->posts->all();
return view('backend.posts.index', compact('posts'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\View\View
*/
public function create()
{
return view('backend.posts.create');
}
/**
* Store a newly created resource in storage.
*
* @param PostRequest $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function store(PostRequest $request)
{
$this->savePost($request->all());
return redirect()->route('admin.posts.index');
}
/**
* Display the specified resource.
*
* @param int $id
*
* @return \Illuminate\View\View
*/
public function show($id)
{
$post = $this->posts->find($id);
return view('backend.posts.show', compact('post'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
*
* @return \Illuminate\View\View
*/
public function edit($id)
{
$post = $this->posts->find($id);
return view('backend.posts.edit', compact('post'));
}
/**
* Update the specified resource in storage.
*
* @param PostRequest $request
* @param int $id
*
* @return \Illuminate\Http\RedirectResponse
*/
public function update(PostRequest $request, $id)
{
$this->savePost($request->all(), $id);
return redirect()->route('admin.posts.index');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
*
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy($id)
{
$this->posts->delete($id);
return redirect()->route('admin.posts.index');
}
/**
* We create or update the Post.
*
* @param array $data
* @param int|null $id
*
* @return Post
*/
protected function savePost(array $data = [], $id = null)
{
// Image Handling
if (isset($data['image'])) {
$data['image'] = $this->buildImage($data['slug'], $data['image']);
}
// We create the Post
if ($id === null) {
$data['author_id'] = Auth::id();
$post = $this->posts->create($data);
} else {
$post = $this->posts->update($data, $id);
}
$this->saveTags($data, $post);
}
/**
* Build the image.
*
* @param string $slug
* @param UploadedFile $image
*
* @return string
*/
protected function buildImage($slug, $image)
{
$filePath = '/uploads/posts/'.$slug.'.'.$image->getClientOriginalExtension();
Image::make($image)->save(public_path($filePath));
return $filePath;
}
/**
* Save the tags for the Post.
*
* @param array $data
* @param $post
*/
protected function saveTags(array $data, $post)
{
$tags = explode(',', $data['tags']);
foreach ($tags as $tag) {
Tag::firstOrCreate(['name' => $tag, 'slug' => str_slug($tag)]);
}
$post->tags()->sync($this->tags->findWhereIn('name', $tags)->pluck('id')->toArray());
}
}