Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

How to get only specific attributes with from Laravel Collection?

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.

IlluminateDatabaseEloquentCollection {#1083 ▼
    #items: array:2 [▼
    0 => AppModelsCart {#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 => AppModelsCart {#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 .

question from:https://stackoverflow.com/questions/65934311/how-to-get-only-specific-attributes-with-from-laravel-collection

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...