Get collection of relation of Laravel Eloquent Collection

Viewed 42

I have Article and Category models. A Category can have many Article. I select all active Article with as below:

 $list = Article::with('category')->whereActive('Y')->get();

Is there anyway to get all Category models in $list collection? Thanks for supporting. PS: I am using Laravel 8.36

1 Answers

you can use pluck to get the categories list:

$list = Article::with('category')->whereActive('Y')->get();
$categories=$list->pluck('category');
Related