简体   繁体   中英

Laravel API ResourceCollection WhenLoaded

I try to include a relationship in my resource array if it has been eager loaded, but don't get it working.

Anyone has an idea, how I can check the relationships in the ResourceCollection?

Database schema looks like this: 在此处输入图像描述

Here is my Post Model

class Post extends Model
{
    function categories() {
        return $this->belongsToMany('App\Category');
    }
}

Here is my Category Model

class Category extends Model
{
    function posts() {
        return $this->belongsToMany('App\Post');
    }
}

Here is my Post Controller

Class PostController extends Controller
{
    public function index()
    {
        return new PostResourceCollection(Post::with("categories")->get());
    }
}

Here is my Post ResourceCollection

class PostResourceCollection extends ResourceCollection
{
    public function toArray($request)
    {

        return [
            'data' => $this->collection->transform(function($page){
                return [
                    'type' => 'posts',
                    'id' => $page->id,
                    'attributes'    => [
                        'name' => $page->title,
                    ],
                ];
            }),
            //'includes' => ($this->whenLoaded('categories')) ? 'true' : 'false',
            //'includes' => ($this->relationLoaded('categories')) ? 'true' : 'false',
        ];
    }
}

Maybe too late, below solution is a workaround for this case:

return [
    ...,
    'includes' => $this->whenLoaded('categories', true),
];

Loading custom attribute:

return [
    ...,
    'includes' => $this->whenLoaded('categories', fn() => $this->categories->name),
];

You relationship is wrong, a post belongs to many categories while a category has many posts so change:

class Category extends Model
{
    function posts() {
        return $this->belongsToMany('App\Post', 'category_post');
    }
}

to

class Category extends Model
{
    function posts() {
        return $this->hasMany('App\Post', 'category_post');
    }
}

Now when you load the post you can load the categories also:

$posts = Post::with('categories')->get();

got it.. That was the missing piece. if anyone has a better solution for this it would be much appreciated.

      foreach ($this->collection as $item) {
          if ($item->relationLoaded('categories')) {
              $included = true;
          }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM