dimanche 30 juin 2019

How to save multiple images in database Laravel?

I'm trying to save multiple images of products in database. I created the images table and set up the relationship with products table.

Here is how my store function in controller looks like

public function store(Request $request) 
{ 
    $formInput=$request->all();
    $image=array();
    if($files=$request->file('image')){
        foreach($files as $file){
            $name=$file->getClientOriginalName();
            $file->move('images',$name);
            $image[]=$name;

        }
    }

    //dd($formInput);

    Product::create(array_merge($formInput,
   [
   // 'product_id'=>$product->id, 
     'image' => what to put here 
     'seller_id'=> Auth::user()->id,
    ])); 
    return redirect()->back(); 

here is image model

class Image extends Model
{
//

protected $table='images';
protected $fillable=['product_id','image'];

public function product()
{
  return $this->belongsTo('App\Product','product_id');
 }
}

here is product model

class product extends Model
{
  protected $table='products';
  protected $primaryKey='id';
  protected $fillable= ['seller_id','pro_name','pro_price','pro_info','stock','category_id'];

  public function images()
  {
   return $this->hasMany('App\Image', 'product_id');
  }

}

When i dd($formInput); i'm seeing all the details including images but how do i submit them to the database? (images to images table and products details to products table)



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2FGRPo4
via IFTTT

Aucun commentaire:

Enregistrer un commentaire