vendredi 28 juin 2019

How to store multiple images in database and show them in the view Laravel

I'm trying to store multiple images in database and also show them in the view. I have products table and images table which are related, in the images table I have a foreign key named(image). So far it just store and show only one image instead of multiple.

Here are the codes

Controller

  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;
            $formInput['image']=$name; 
        }

    }

Blade

  <input type="file" name="image[]" multiple class="form-control">

Product.php

   public function products()
 {
    return $this->belongsTo('App\Images', 'image');
  }

Images.php

   public function images()
   {
     return $this->hasMany(Product::class, 'image');
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire