This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Daniel81
Daniel81

Apologies if this is a simple request, I'm still learning laravel as well as the ins & outs of October.

My question is:

I've created a simple Products plugin where you can enter products into a table in the DB, with each product containing a Title, Description & Image (for now), and the form model uses a fileupload field, but how do you attach/relate the uploaded image to the created product?

The image uploads fine to the public folder, and appears in the image preview when updating the product, but I just can't figure out how to join the two together so that when I display the products on the page I can pull through the related image also. It may be that they're already joined together in some way since the image appears when you update the product in the preview, but I'm not sure how to pull the image if that's the case.

Any help would be appreciated :)

Shahiem
Shahiem

$model = Model::find(PRODUCTID); echo $model->image->getPath();

Last updated

Daniel81
Daniel81

@Shahiem

Thanks for this, however it doesn't seem to return any image.

In my component in the onRun function I'm using $model = productModel::all(); to output all the products, but there is no image assigned to the products, even though the uploaded image is showing on the product update page. If I try to run $model = productModel::find(1); echo $model->image->getPath();, I just get the error Call to a member function getPath() on a non-object.

What am I missing? How do I connect the uploaded image to the Product so I can display them both on the page?

Last updated

Shahiem
Shahiem

Can I see the code of the Product model? So I can help you :)

Last updated

Daniel81
Daniel81

I've managed to get it working :) The issue all along was that I'd created an image field in the DB table that was overwriting the image from the model, and as soon as I'd deleted the image field from the table, it all worked.

@Shahiem, thanks for your help, the above code you posted is now working for me :)

Last updated

Shahiem
Shahiem

Nice! No problem :)

1-6 of 6

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.