/home/mip/mip/app/Modules/Client/Repositories/Posts/PostCategoryRepository.php
<?php

namespace QxCMS\Modules\Client\Repositories\Posts;
use Illuminate\Support\Str;
use QxCMS\Modules\AbstractRepository;
use QxCMS\Modules\Client\Models\Posts\PostCategory;
use QxCMS\Modules\Client\Repositories\Posts\PostsRepositoryInterface as Posts;

use Illuminate\Http\Request;

class PostCategoryRepository extends AbstractRepository implements PostCategoryRepositoryInterface
{

    protected $model;

    function __construct(PostCategory $model, Posts $posts)
    {
        $this->model = $model;
        $this->posts = $posts;
    }

    public function create(array $request)
    {
        $postCategory = $this->model->fill($request);
        $postCategory->slug = Str::slug($request['name']);
        $postCategory->save();
        return $postCategory;
    }

    public function update(Request $request)
    {
        $id = $request->input('id');
        $name = $request->input('name');

        $postCategory = $this->findById($id);
        $postCategory->name = $name;
        $postCategory->slug = Str::slug($name);
        $postCategory->save();
        return $postCategory->name;
    }

    public function delete($id)
    {
        $postCategory = $this->findById($id);
        if ($postCategory->post->count() > 0) {
            return response()->json(['error' => 'exists', 'id' => $id]);
        } else {
            $postCategory->delete();
            return response()->json(['success' => 'yes', 'id' => $id]);
        }            
    }

    public function postCategoryList()
    {
        return
            $this
            ->model
            ->orderBy('name','asc')
            ->pluck('name', 'id')
            ->toArray();
    }
}