lundi 9 novembre 2015

insert into childe table laravel

I'm trying to insert an image of a product into a child table called product_images, but it keeps trying to find the column 'path' in the products table.

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'path' in 'field list'

Please help! Thanks

Here's how I have my controllers/models set up.

PRODUCT MODEL

public function category()
{
   return $this->belongsTo('App\Category');
}

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

PRODUCT IMAGES

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

PRODUCT CONTROLLER

$product = new Product(); $product->category_id = $request->category_id; $product->name = $request->name; $product->price = $request->price;

//upload image
if ($request->hasFile('image')) {
    //validate
    $this->validate($request, [
        'image' => 'mimes:jpeg,bmp,png'
    ]);

    $image = $request->image;
    $filename = time() . '-' . $image->getClientOriginalExtension();
    $path = public_path('img/products/' . $filename);
    Image::make($image->getRealPath())->resize(468, 249)->save($path);
    $product->path = 'img/products/' . $filename;
}

$product->images()->save($product);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire