1myb
1myb

Reputation: 3596

Access data efficiently in Laravel

Would like to know is it possible to have a better way to get the data from the database easily via eloquent? The following is the code i use usually, and thinking of get all records match to the shopID but it would be inefficient in filter and access in the loop.

$add1 = ShopMeta::where('shopId', '=', $theID)->where('metadataKey', '=', 1015)->firstOrFail();

The benefit of this is i can access the member with the following instead of foreach loop.

$add1->metadataValue;

Is there any better methods to do the job of get all the value?

Upvotes: 0

Views: 170

Answers (2)

rmobis
rmobis

Reputation: 27021

Just use a whereIn:

$add1 = ShopMeta::where('shopId', '=', $theID)
                ->whereIn('metadataKey', array(1015, 1016))
                ->get();

Upvotes: 0

Hao Luo
Hao Luo

Reputation: 1891

This is what Scoped Query is for.

Upvotes: 3

Related Questions