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!