samedi 29 juin 2019

how to join two tables in database Laravel

I'm trying to join two tables (products table and Images table), so in products table each product has multiple images(stored in a row named image) so I want to display images of each product in images table. In images table I have a foreign key named product_id references(id) on products table, The problem is it can't store images in images table,(it keeps storing images in the row(image) in products table). How do I solve this?

Codes

Product.php

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

Image.php

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

Controller

 public function store(Request $request) 
 { 

$Input=$request->all();
$image=array();
if($files=$request->file('image')){
foreach($files as $file){
    $name=$file->getClientOriginalName();
    $file->move('images',$name);
    $image[]=$name;
  }
  } 
 product::create(array_merge($Input,
 [
 'image' => json_encode($image),

 ])); 
return redirect()->back(); 

}

Any help will be appriciated.



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

Aucun commentaire:

Enregistrer un commentaire