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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…