PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 8, 2022

[FIXED] Loading a belongs to relationship inside of a laravel resource collection

 March 08, 2022     laravel     No comments   

Issue

I'm having some issues loading my relationships into a ResourceCollection to be consumed by an API, I want to load blogs that each belong to a category.

The blog model which uses a belongsTo relationship

    <?php
    namespace App\Models;

    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Database\Eloquent\Relations\BelongsTo;
    use Illuminate\Database\Eloquent\SoftDeletes;

    class BlogPost extends Model {
        use HasFactory, SoftDeletes;

        protected $fillable = [
            'title',
            'content',
            'seo_title',
            'seo_content',
        ];

        public function categories(): BelongsTo {
            return $this->belongsTo(BlogCategory::class);
        }
    }

The Category model has a hasMany to blogs

    <?php
    namespace App\Models;

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

    class BlogCategory extends Model {
        use HasFactory;

        protected $fillable = [
            'slug'
        ];

        public function blogs(): HasMany {
            return $this->hasMany(BlogPost::class);
        }
    }

Inside of the blog_post migration, I added a foreign key to blog_categories

    $table->foreignId('category_id')->constrained('blog_categories');

Then, in my BlogPost ResourceCollection I tried loading the relationship,

    #[ArrayShape(['data' => "\Illuminate\Support\Collection", 'category' => AnonymousResourceCollection::class])] public function toArray($request): array {
        return [
            'data' => $this->collection,
            'category' => BlogCategoryCollection::make($this->whenLoaded($this->categories))
        ];
    }

I call the collection inside of the index function of my controller

    public function index(): BlogPostCollection
    {
        return new BlogPostCollection(BlogPost::all());
    }

And when I hit the api/blogs endpoint I get the error :

    Property [categories] does not exist on this collection instance. 

Solution

Managed to fix it in the end.

Changed the BlogPostResourceCollection to the following

       return [
            'data' => $this->collection,
            'categories' => BlogCategoryCollection::collection($this->whenLoaded('categories'))
        ];

seems to work in the end.



Answered By - niels van hoof
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing