jake.admin@gmail.com's avatar

Help with Relations: one product has many images

Hello

I seem to be having troubles with relation... I have a grasp on the concept I just can't seem to get it to all fit together.

I have "Products" which contain one or more "ProductImages" I have the multifile upload working, the product_images table correctly associates image_ids with product_ids. I have what I believe is usually referred to as a pivot table, product_images , which contains an id, product_id, and image (path). When files are uploaded, it links them correctly to the product_id to which they belong.

I just cant seem to pull multiple images from the table when displaying the Product page.

here is my ProductImage class

class ProductImage extends Model
{
    protected $fillable = [
        'product_id',
        'image'
    ];

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

My Product class has the association:

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

My ProductController:

class ProductController extends Controller
{
    public function show($id)
    {
        $product = Product::findOrFail($id);
        return view('products.productsDetail')
            ->with('product', $product);
    }
}

I assume I should be able to access the images in the same way I would access other relational data. For instance, products have a seller, which is tied to a user table and they have a category which is in a separate categories table. I can pull the product name, seller's name and product category in blade with:

Item Name: {{ $product->name }}
Seller: {{ $product->user->name }}
Category: {{$product->category->products}}

but these are kind of a one-to-one relationship. One product has one seller and one category. I just can't sort out the relationship where one product has many images.

I followed [this tutorial] (http://laraveldaily.com/upload-multiple-files-laravel-5-4/) to help me set up the multiple file upload and storage.

I'm just kind of lost here and not sure what I'm missing. I would greatly appreciate some help! If you need anymore information, I will be happy to provide.

Thank you!

0 likes
5 replies
tykus's avatar
tykus
Best Answer
Level 104

You don't really have a pivot table in this case since there appears to be no associated images table.

Your productImages relation should return a Collection of ProductImage instances, which you can iterate over in your view, e.g.

@foreach($product->productImages as $image)
  <img src="{{ $image->image }}">
@endforeach
2 likes
jake.admin@gmail.com's avatar

@tykus You are the man! Thank you so much! That works but now I can't get my images to load up from their saved location. I had a previously working single file uploader, which saved images to ...\public\upload\file.jpg, the page I linked to in OP that I followed for multi-file is saving them in ...\storage\app\upload\filename.jpeg I know artisan has the symlink creator: php artisan storage:link but I'm on Windows so I cannot symlink. Is there a workaround or how should I go about fixing the file location? Thanks again!

jake.admin@gmail.com's avatar

Just an update, php artisan storage:link doesn't work on my setup. I'm using Vagrant/Homestead on VirtualBox with a Windows 10 Pro host.

I found a Windows command that works for symlinks: mklink /D public\upload ..\storage\app\upload Make sure to restart your vagrant box.

Hopefully that will help someone in the future :) Thanks again @tykus

Cronix's avatar

@jake.admin@gmail.com

SSH into the homestead vm. Run the artisan link command from there (in your project root). Run all artisan related commands from there, as well as npm run dev, etc. Don't do it from the windows machine. Do it from the homestead box. The only thing you should do from your windows pc is the coding. Don't run commands from it.

jake.admin@gmail.com's avatar

I was SSH'd into my vagrant box when I ran the artisan link command, and it failed symlink(): Protocol error

Please or to participate in this conversation.