Reputation: 2000
i have a 1 to n relation ship between product and images and on images i have a field called color_id
which has a 1 to n with images . now what i want to do is to call the product with images relationship like below :
$relation = Product::with('images')->where('id',$product->id)->get();
dd($relation);
now what want is to get 1 images from each color_id
so for example if this product has 3 images
with color_id
of 1 , and 4 images with color_id
of 2 .i want to get 1 images from each color . and here is the dd result of $relation
in case needed .
^ Illuminate\Database\Eloquent\Collection {#945 ▼
#items: array:1 [▼
0 => Webkul\Product\Models\Product {#912 ▼
#fillable: array:4 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [▶]
#original: array:7 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"images" => Illuminate\Database\Eloquent\Collection {#968 ▼
#items: array:3 [▼
0 => Webkul\Product\Models\ProductImage {#967 ▼
+timestamps: false
#fillable: array:4 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:6 [▶]
#original: array:6 [▼
"id" => 17
"type" => null
"path" => "product/39/y0ueAFEgscD9ZsVFg7nT2WPNsU6vDyXWqLVONa8L.jpeg"
"product_id" => 39
"product_color_id" => 3
"product_size_id" => null
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
1 => Webkul\Product\Models\ProductImage {#970 ▼
+timestamps: false
#fillable: array:4 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:6 [▶]
#original: array:6 [▼
"id" => 23
"type" => null
"path" => "product/39/M9JTVsy2BtNp8LuUQh7cp17S5jN1ifOZ1P9WZk3h.jpeg"
"product_id" => 39
"product_color_id" => 2
"product_size_id" => null
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
2 => Webkul\Product\Models\ProductImage {#969 ▼
+timestamps: false
#fillable: array:4 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:6 [▶]
#original: array:6 [▼
"id" => 24
"type" => null
"path" => "product/39/STA9sGKjWHWDn0wkmnVRqZYdI2adLE5qaIn7vyFQ.jpeg"
"product_id" => 39
"product_color_id" => 1
"product_size_id" => null
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
}
Upvotes: 1
Views: 57
Reputation: 6005
Try this
$relation =Product::with(array('images' => function($query) {
$query->groupBy('product_color_id');
})->where('id',$product->id)->get();
Upvotes: 1