Liza
Liza

Reputation: 129

Laravel axios listing relational data

I am making an axios request to the address I specified below with Laravel. I want the incoming data to come as follows. I want to list the data with language en from the categories table and list the data matching (language) with the category_id in the pages table, but I was not successful. Where am I doing wrong?

http://localhost:3000/categories?lang=en

Mysql Table & Model

Mysql Table

SELECT * FROM `categories`
id
title
slug
language

Model
class Category extends Model
{
    use HasFactory;

    public function pages(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(Page::class, 'category_id', 'id');
    }
}

Mysql Table & Model

Mysql Table

SELECT * FROM `pages`
id
title
slug
content
category_id
language

Model
class Page extends Model
{
    use HasFactory;
}

Routing

Route::prefix('categories')->group(function () {
    Route::get('', [CategoryController::class, 'index']);
});

CategoryController

public function index()
    {
       return $category = Category::where('language', 'en')->with('pages')->get();
    }

output

[
    {
        "id": 2,
        "title": "corporate",
        "slug": "corporate",
        "seo": null,
        "order": "0",
        "status": "1",
        "language": "en",
        "created_at": "2022-04-04T13:31:27.000000Z",
        "updated_at": "2022-04-04T13:31:27.000000Z",
        "pages": null
    },
    {
        "id": 4,
        "title": "new category",
        "slug": "new -category",
        "seo": null,
        "order": "0",
        "status": "1",
        "language": "en",
        "created_at": "2022-04-08T12:23:57.000000Z",
        "updated_at": "2022-04-08T12:23:57.000000Z",
        "pages": null
    }
]

what i want

[
    {
        "id": 2,
        "title": "corporate",
        "slug": "corporate",
        "seo": null,
        "order": "0",
        "status": "1",
        "language": "en",
        "created_at": "2022-04-04T13:31:27.000000Z",
        "updated_at": "2022-04-04T13:31:27.000000Z",
        "pages": null,
        "children": [
            {
                "id": "1",
                "title": "hello word",
                "slug": "hello-word",
                "content": "",
                "category_id": "2",
                "language": "en"
            }
        ]
    },
    {
        "id": 4,
        "title": "new category",
        "slug": "new -category",
        "seo": null,
        "order": "0",
        "status": "1",
        "language": "en",
        "created_at": "2022-04-08T12:23:57.000000Z",
        "updated_at": "2022-04-08T12:23:57.000000Z",
        "pages": null,
        "children:": []
    }
]

Upvotes: 0

Views: 63

Answers (1)

geertjanknapen
geertjanknapen

Reputation: 1451

Assuming a page has one category, and a category has many pages, your relationship seems to be incorrect.

Change it to:

class Category extends Model
{
    use HasFactory;

    public function pages(): \Illuminate\Database\Eloquent\Relations\hasMany
    {
        return $this->hasMany(Page::class, 'category_id', 'id');
    }
}

and in page model, add:

class Page extends Model
{
    use HasFactory;

    public function category(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(Category::class, 'id', 'category_id');
    }
}

then you should be able to use ->with('pages') on your Category model

Upvotes: 1

Related Questions