Michael George
Michael George

Reputation: 338

Elquent Relation Limit records

I'm trying to send API with certain number of records

$category->products

I tried to use limit() or take() but it fails so is there any smart solution than go to pivot and select it with limit ??

Upvotes: 0

Views: 47

Answers (2)

Michael George
Michael George

Reputation: 338

I found the solution I was easy

$category->products()->limit(2)->get()->all()

that's all

Upvotes: 1

Hayreddin Tüzel
Hayreddin Tüzel

Reputation: 949

Copy data to a new collection.

$collection =$category->products;

you can now send the number of times you want using take ().

for example;

$collection->take(5);

if your question is different please explain it more clearly.

Upvotes: 2

Related Questions