%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/evablog/app/Http/Controllers/Api/
Upload File :
Create Path :
Current File : /var/www/html/evablog/app/Http/Controllers/Api/PostController.php

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Post;
use App\Transformers\PostTransformer;
use Illuminate\Http\Request;

class PostController extends Controller
{

    /**
     * @param Request $request
     */
    public function index(Request $request)
    {
        $posts = Post::query()->orderBy('created_at', 'desc')->paginate(15);

        return response()->json(fractal($posts, new PostTransformer)->toArray());
    }


    /**
     * Show the application dashboard.
     *
     * @param $post
     * @param $category
     * @return \Illuminate\Http\Response
     */
    public function attachCategory($post, $category)
    {
        /** @var Post $post */
        $post = Post::query()->find($post);
        /** @var Category $category */
        $category = Category::query()->find($category);
        $post->categories()->attach($category);
        return response()->json(fractal($post, new PostTransformer)->toArray());
    }

    /**
     * @param $post
     * @param $category
     * @return \Illuminate\Http\JsonResponse
     */
    public function detachCategory($post, $category)
    {
        /** @var Post $post */
        $post = Post::query()->find($post);
        /** @var Category $category */
        $category = Category::query()->find($category);
        $post->categories()->detach($category);
        return response()->json(fractal($post, new PostTransformer)->toArray());
    }

}

Zerion Mini Shell 1.0