codebuilder's avatar

failing to insert into child table with Eloquent

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 MODEL

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;
    }
   // dd($product);
    $product->images()->save($product);
0 likes
7 replies
andremellow's avatar

What are you tryng to do with ? $product->images()->save($product);

$product->images() represents a collection of Imagens, so you need to pass Image Class, not Product.

something like this:

( Im my case I have one plublication with many Media. )


//You can create your product if is the case
$publication = publication::find($publication_id);
      
           //Save file in the disk

            //Create imagem ( media in my case)
            $media = new Media([
                'file' => $file->getClientOriginalName(), 
                'full_path' => $path.$fileName,
                'thumbnail' => MediaRepository::getThumbnailPath( $mediaTypeId , $extension , $path , $fileName ) ,
                'media_type_id' => $mediaTypeId
            ]);

            // Add media to Publication
            $publication->media()->save($media);

codebuilder's avatar

I'm trying to save the product_id in the product_image table (which is a child table of the product table)

fraserk's avatar

Also your pivot table should be named image_product

Snapey's avatar

There is no need for a pivot table if its one to many?

thomaskim's avatar

Well, there are several problems so I'm not sure where to start. You should get things working one-by-one and then gradually add features.

For example, you instantiated the product: $product = new Product();, but you never save it to the database. How can you save an image to the database with a product_id if the product doesn't even have an id because it's not being saved to the database?

Second, $product->images()->save($product); doesn't work because you should be passing the image instance there.

Third, you never even created an image object. I'm assuming that this Image class: Image::make($image->getRealPath())->resize(468, 249)->save($path); is the Intervention image class, not your Image model so there isn't even an Image instance to save to the database.

andremellow's avatar

@codebuilder ,

 I'm assuming that you have N images for each product, right? If so, your tables should looks like ( skipping some fields):

First you need so create a product record:

$product = new Product;

$product->name = .....
...
...
$product->save();

Than, you can create the image "inside" the product.

As you have the relationship, eloquent is smart enough to you do like this:

$product->images()->create([ 'xxx' => 'xxxx', 'xxx' => 'xxxx', ]);

 By doing this, eloquet will create the image record associted at this product. 
codebuilder's avatar
codebuilder
OP
Best Answer
Level 2

I figured it out guys--turns out I need to create a new product_image object.

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

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

And then do this: $product->images()->save($image);

Thanks everyone for your help.

Please or to participate in this conversation.