In Laravel 5.6 i have a polymorphic relationship .
I have many products in db and avatars for each product .
When i send products data to view i want to send avatar for every product .
I write a polymorphic relationship between avatars table and products table and i can access avatar of each product using this code easily :
$avatar = Product::find(id)->avatars()->first();
But when db result is NOT one product and is a array of products how i can access each avatar ?
If i have result like this how can i find avatar of each product in view file ? :
$products = Product::all();
return view('frontend/home')->with([
'products'=>$products,
]);
And in View i have something like this :
#foreach($products as $product)
<li>
#include('items.product')
</li>
#endforeach
You should be able to use with():
$products = Product::with('avatars')->get();
Then in your blade file you would have something like:
#foreach($products as $product)
<img src="{{ $product->avatars->first()->url }}" alt="" />
#endforeach
Related
want to show subcategories from the same categories on the Images page
Subcategory Database- https://prnt.sc/26cr0jl Images Database - https://prnt.sc/26cr0t8
Demo -https://cbeditz.com/photo/15384/yellow-cb-editing-background-full-hd-download
#foreach( App\Models\SubCategories::where('categories_id', '=', 'images->categories_id')->get() as $subcategories )
Considering you have relationship setup between Category and SubCategory Models
You should pass these variables from a controller.
In your controller where you are loading view and you have category id coming from a route request.
$sub_categories = Category::with('sub_categories')->find($category_id);
return view('your-view', compact('sub_categories'));
And in your view you can use it like
#foreach($sub_categories as $sub_category)
{{ $sub_category->name }}
#endforeach
I have this huge problem that I can't find a workaround. I'm building an e-commerce app with Laravel and it's essential to build a good filtering system for products. I have products and attributes table with a many-to-many relation. Pivot table product_attribute has additional value (if the attribute is color, value would be red for example) column. When entered to the shop page there is a sidebar with the filtering options. The only option I can show there is brand since it's a one-to-many relation with the product table. What's the right way to show these attributes. As I mentioned attributes should be dynamic to the products on the page. Different products in different categories (bikes, clothes, balls, pool tables) may have different attributes.
public function show($slug){
// Get products with attributes for specic
$category = Category::with('products')->where('slug',$slug)->firstOrFail();
// Collect brand ids
$b_ids = array_filter($category->products->pluck('brand_id')->toArray());
//Get brands to show in the filtering options
$brands = Brand::whereIn('id',$b_ids)->get();
$attributes = ?
return view('front.shop', compact('category','brands', 'attributes'));
}
Eager load attributes with products - Category::with('products.attributes')
public function show($slug){
// Get products with attributes for specic
$category = Category::with('products.attributes')->where('slug',$slug)->firstOrFail();
// Collect brand ids
$b_ids = array_filter($category->products->pluck('brand_id')->toArray());
//Get brands to show in the filtering options
$brands = Brand::whereIn('id',$b_ids)->get();
return view('front.shop', compact('category','brands'));
}
Then in the view attributes can be accessed via the product
that's not what I want. It just gives me the value. I want attribute name to be displayed at the top (for example COLOR) and values (Red, Blue, etc) at the bottom next to a checkbox for the user to filter
Assuming you are storing all possible values for an attributes as options
// ...
#foreach($categories as $category)
#foreach($category->products as $product)
#include('attribute-options', ['attributes' => $product->attributes])
#endforeach
#endforeach
// ...
Extract attributes display for a product to a partial - attribute-options.blade.php
#foreach($attributes as $attribute)
<h3>{{ $attribute->name }}
<ul>
#foreach($attribute->options as $option)
<li>
<label for="{{$attribute->name}}-{{$option}}">{{ $option }}
<input
type="radio"
value="{{$option}}"
id="{{$attribute->name}}-{{$option}}"
name="{{$attribute->name}}-{{$option}}"
/>
</label>
</li>
#endforeach
</ul>
#endforeach
I have a prolem here with 2-level far relations between models. It's classic eCommerce website with expected structure where you have a product table, galleries and images. Product and Image has gallery_id so every gallery has potencialy many Images. This is my solution which doesn't work. Returns "Trying to get property 'images' of non-object".
VIEW Product.blade.php
<ul>
#foreach ($product->gallery->images as $image)
<li><img src="{{ asset($image->path) }}" alt="" data-image="{{ asset($image->path) }}"></li>
#endforeach
</ul>
MODEL Product.php
public function gallery()
{
return $this->belongsTo('App\Gallery');
}
MODEL Gallery.php
public function images()
{
return $this->hasMany('App\Image');
}
Why I can't access images from product view by $product->gallery->images?
I have a hasmany relationship on my model and I'm trying to output just the one result, i have a product category which can display only one product image.
I have two tables.
1 = Product
2 = ProductPhotos
I've tried outputting the one photo like
#foreach($products as $product)
<img src="{{ $product->photos->first() }}">
#endforeach
I have the following relationship setup in my product model
public function photos()
{
return $this->hasMany('App\ProductPhoto', 'product_id');
}
but this doesnt work.
You're just missing the parenthesis for the method. It should be:
$product->photos()->first();
Which will allow Eloquent to access the photos method in the Product model.
I'm trying to show a structure using as example the tables showed in this thread:
Laravel Eloquent Filter By Column of Relationship
Using this I would like to show in one page (index.blade.php for example) something like this:
Category 1
Post1.Category1
Post2.Category1
Category 2
Post1.Category2
Post2.Category2
Post3.Category2
Post4.Category2
Category 3
Post1.Category3
I don't know what exactly which arrays to I need to pass from Controller.
I can get the list of Categories but how the post for each category???
Thanks
Any idea
You just need to pass in the Categories with their Posts. It's pretty straightforward.
Controller
$categories = Category::with('posts')->get();
return View::make('some.view')->with('categories', $categories);
View
#foreach($categories as $category)
{{{ $category->name }}}
#foreach($category->posts as $post)
{{{ $post->name }}}.{{{ $category->name }}}
#endforeach
#endforeach