Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

abhishekregmi's avatar

how to load realationship in api laravel ?

i have made laravel api and want to display category name as a relationship. i want only category name but i get id along this inside braces like below:

{
  "message": "blog successfully fetched!",
  "data": [
    {
      "id": 5,
      "title": "abhishek",
      "description": "<p>my world\t</p>",
      "category": {
        "id": 1,
        "category": "abhishek"
      },
      "publishedDate": "2022-09-06",
      "tags": "test",
      "metaDescription": "mero",
      "image": "/uploads/1663234220.png",
      "isActive": 0,
      "created_at": "2022-09-15T09:30:20.000000Z",
      "updated_at": "2022-09-16T09:46:58.000000Z",
      "slug": "j"
    }
  ],
  "pagination": {
    "total": 1,
    "perPage": 10
  }
}

this is my model:

class Blog extends Model
{
    use HasFactory;
    protected $with=['category'];
	protected $guarded = ['id','created_at','updated_at'];
    public function category()
    {
      return $this->belongsTo(BlogCategory::class,'category')->select('id','category');
    }
}
0 likes
1 reply

Please or to participate in this conversation.