CastCast
CastCast

Reputation: 11

Pivot table pagination doesn't work in laravel 8

I am doing an assignment for my study, here the code

Category Models

It have books method that pivot table Books and Categories

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $fillable = [ 'name', 'slug', 'image', 'status' ];

    public function books()
    {
        return $this->belongsToMany(Book::class, 'book_category', 'category_id', 'book_id');
    }
}

Category Resource

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class Category extends JsonResource
{
    public function toArray($request)
    {
        $parent = parent::toArray($request);
        $data['books'] = $this->books()->paginate(6);
        $data = array_merge($parent, $data);
        return [
            'status' => 'succes',
            'message' => 'category data',
            'data' => $data
        ];
    }
}

Category Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Category;
use App\Http\Resources\Category as CategoryResource;

class CategoryController extends Controller
{
    public function slug($slug)
    {
        $criteria = Category::where('slug', $slug)->first();
        return new CategoryResource($criteria);
    }
}

Book Resource

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Book extends Model
{
    protected $fillable = [
        'title', 'slug', 'description', 'author', 'publisher', 'cover',
        'price', 'weight', 'stock', 'status'
    ];
}

And here the result from postman

{
    "status": "succes",
    "message": "category data",
    "data": {
        "id": 7,
        "name": "debitis",
        "slug": "debitis",
        "image": "/xampp/htdocs/bookstore-api/public/images/categories\\518c73c94a9f2ca6524c5c2ab8dc02ae.png",
        "status": "PUBLISH",
        "created_at": "2023-07-16T08:39:41.000000Z",
        "updated_at": null,
        "books": {
            "current_page": 1,
            "data": [],
            "first_page_url": "http://bookstore-api.test/v1/categories/slug/debitis?page=1",
            "from": null,
            "last_page": 1,
            "last_page_url": "http://bookstore-api.test/v1/categories/slug/debitis?page=1",
            "links": [
                {
                    "url": null,
                    "label": "&laquo; Previous",
                    "active": false
                },
                {
                    "url": "http://bookstore-api.test/v1/categories/slug/debitis?page=1",
                    "label": "1",
                    "active": true
                },
                {
                    "url": null,
                    "label": "Next &raquo;",
                    "active": false
                }
            ],
            "next_page_url": null,
            "path": "http://bookstore-api.test/v1/categories/slug/debitis",
            "per_page": 6,
            "prev_page_url": null,
            "to": null,
            "total": 0
        }
    }
}

The first time I run the script I got an SQL error because book_category table was not yet created, then after the table created, the result like the above I expect books pagination based on category will be working

Upvotes: 0

Views: 46

Answers (0)

Related Questions