0

How can I get all specific IDs from the collection in Laravel 8 ??

I'm trying to get food_item_id from collection by foreach loop. But I only get first item ID. I want not only the first item but also all item Ids.

I want to get all food_item_id form here.

Illuminate\Database\Eloquent\Collection {#1083 ▼
    #items: array:2 [▼
    0 => App\Models\Cart {#1082 ▼
      #guarded: []
      #connection: "mysql"
      #table: "carts"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:8 [▼
        "id" => 265
        "food_item_id" => 6
        "user_id" => 3
        "quantity" => 3
        "price" => 124
        "total_price" => 372
        "created_at" => "2021-01-28 08:22:16"
        "updated_at" => "2021-01-28 08:51:51"
      ]
      #original: array:8 [▶]
      #changes: []
      #casts: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
    }
    1 => App\Models\Cart {#1291 ▼
      #guarded: []
      #connection: "mysql"
      #table: "carts"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:8 [▼
        "id" => 267
        "food_item_id" => 4
        "user_id" => 3
        "quantity" => 1
        "price" => 179
        "total_price" => 179
        "created_at" => "2021-01-28 08:51:54"
        "updated_at" => "2021-01-28 08:51:54"
      ]
      #original: array:8 [▶]
      #changes: []
      #casts: []
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
    }
  ]
}

I did this

$food_item_ids = $food_item_id->pluck('food_item_id')->all();

I get an array of ID, Now I want to find this

 $food_item = FoodItemPrice::where('food_item_id', $food_item_ids)->get();

I want to find FoodItemPrice ID which match with food_item_ids .

2
  • Post your code that you have tried? Commented Jan 28, 2021 at 9:33
  • Please post the expected result. Commented Jan 28, 2021 at 9:33

2 Answers 2

1

You're working with a collection which Laravel provides some very handy helper methods for. The one you're specifically after would be pluck.

$food_item_ids = $collection->pluck('food_item_id')->all();

Update

Assuming $food_item_ids in the above has a collection of ids and you want to use them to find your $food_items using those ids, then you could do:

$food_items = FoodItemPrice::whereIn('food_item_id', $food_item_ids)->all());
Sign up to request clarification or add additional context in comments.

2 Comments

It shows this error Call to undefined method Illuminate\Database\Eloquent\Builder::all()
Is FoodItemPrice an eloquent model?
0

To get all an array of all values of a specific field/attribute of a collection, you can use the pluck()

So you could do something like:

$items = Cart::all();
$foodItemIds = $items->pluck('food_item_id');

https://laravel.com/docs/8.x/collections#method-pluck

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.