vanilophp/framework

image to product

Closed this issue · 2 comments

hi
how we can to import image to product model by php?
`
....
private function getProductBase($str, $state = ProductState::ACTIVE)
{
return Product::where('sku', 'LIKE','%'.trim($str).'%')
->where('state', $state)
->get()->first();
}
....
$product = null;
if ($product = $this->getProductBase($sku)){

  $product
      ->copyMedia($fileUrl)
      ->toMediaCollection('images');

  $product->save();

}
`

by this code, we have new folder in storage, but picture is not relate to product and in media table row is not. :(

The image handling is provided by the Spatie Media library, please consult their docs for details: https://spatie.be/docs/laravel-medialibrary/v10/introduction

Cheers,

ok, my code is correct

ps program runs in liveware section and don't execute finally correct, because i am not visible changes in DB :)