B
B
Boooom2020-06-07 08:35:33
Laravel
Boooom, 2020-06-07 08:35:33

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


and models

public function ximage(){
        return $this->hasOne('App\Http\Models\Site\Images');
    }

    public function ximages(){
        return $this->hasMany('App\Http\Models\Site\Images');
    }


the problem is that it gets images only for the product, where the value is for attach_id == null, and if there is one, it doesn’t find anything

SQL queries like this (we get images for the product with id 1586 - images by id 105

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
  ]


what can be done here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sanes, 2020-06-07
@Sanes

Normal guys don't do that. There can be no copies. If the product differs even slightly, then this is a different product. And the set of attached photos is also different.
Any hot-ass schemes then turn into a bunch of rubbish.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question