Answer the question
In order to leave comments, you need to log in
How to get one picture for several products?
Hello!
There are goods and there are copies of them
For copies, there is a 'attach_id' column in the database - what product the requested product depends on
And if the value for attach_id is indicated in the line, you need to get pictures by this value
Here is the shit code I wrote:
$item_id = $this->config['item_id'];
$attach_id = $this->config['attach_id'];
if($attach_id != null){
$xgoods_id = $attach_id;
} else {
$xgoods_id = $item_id;
}
$product = Goods::with([
'ximage' => function ($query) use ($xgoods_id) {$query->where('goods_id', $xgoods_id)->orderBy('position', 'asc');},
'ximages' => function ($query) use ($xgoods_id) {$query->where('goods_id', $xgoods_id)->orderBy('position', 'asc');}
])
->where('id', $item_id)->where('published', 1)->first();
dd($product);
public function ximage(){
return $this->hasOne('App\Http\Models\Site\Images');
}
public function ximages(){
return $this->hasMany('App\Http\Models\Site\Images');
}
attach_id == null
, and if there is one, it doesn’t find anything 1 => array:3 [▼
"query" => "select * from `images` where `images`.`goods_id` in (1586) and `goods_id` = ? and `images`.`deleted_at` is null order by `position` asc"
"bindings" => array:1 [▼
0 => 105
]
"time" => 0.4
]
2 => array:3 [▼
"query" => "select * from `images` where `images`.`goods_id` in (1586) and `goods_id` = ? and `images`.`deleted_at` is null order by `position` asc"
"bindings" => array:1 [▼
0 => 105
]
"time" => 0.35
]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question