Tommy Hoang
Tommy Hoang

Reputation: 355

Get collection of relation of Laravel Eloquent Collection

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

Upvotes: 2

Views: 137

Answers (1)

OMR
OMR

Reputation: 12188

you can use pluck to get the categories list:

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

Upvotes: 1

Related Questions