How to join pivot table in laravel - laravel

I have 3 tables like this to show all the categories that each story in it. How to make it show all category use join 3 tables. Should i use join or query in view/blade
catogory model
story model
view/blade
structure category
structure story
structure pivot tables

Thank you for the update, given the above, in your Controller I'm assuming you are calling something like Story::all(), you are going to want to eager load the categories with $data = Story::with('categories')->get(), then in your blade file you can loop through the categories and print out the name fields with
#foreach($data as $story)
{{ $story->name }}
#foreach($story->categories as $category)
{{ $category->name }}
#endforach
#endforeach
You can add any additional markup that you need.

Related

How to get data from additional table in many to many relationship?

I have a many to many relationship between tables 'products' and 'recipes'. I created table product_recipe to handle this and my table looks like:
id | recipe_id | product_id | amount
In my blade file I use
#foreach($recipe->product as $row)
<p>{{ $row->name }}</p>
#endforeach
to get a name of used products in recipe. How I can get an amount of product which is stored in product_recipe table? Do I have to create a model for product_recipe table and get it from model?
You will have to declare the relationship wants to include the extra pivot information so that the pivot object that is attached to the records will have more than just the 'id's:
public function products()
{
return $this->belongsToMany(Product::class)->withPivot('amount');
}
Now the pivot object will include the field amount:
foreach ($recipe->products as $product) {
echo $product->pivot->amount;
}
"By default, only the model keys will be present on the pivot object. If your pivot table contains extra attributes, you must specify them when defining the relationship"
Laravel 6.x Docs - Eloquent Relationships - Many To Many - Retrieving Intermediate Table Columns withPivot
You have to use pivot attribute.
#foreach($recipe->product as $row)
<p>{{ $row->pivot->field_name}}</p>
#endforeach
No need to create Model for product_recipe. Just specify table name in relation of two Models.
Like return $this->belongsToMany('App\Product', 'product_recipe')->withPivot('amount'); and similar for other model.

How call polymorphic for each item in loop

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

Laravel Output 1 result from hasmany relationship

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.

How to query data from a pivot table and display it using blade?

I am trying to query data from my pivot table and display it on my view
My View
#foreach ($catalog_downloads as $catalog_download)
<td>
<?php $frequencies = $catalog_download->export_frequencies()->get(); ?>
{{{ $frequencies }}}
</td>
#endforeach
When I do {{{ $frequencies }}} I got this :
[{"id":1,"name":"Semi Annual","created_at":"2015-01-14 15:44:59","updated_at":"2015-01-14 15:44:59","pivot":{"catalog_download_id":104,"export_frequency_id":1}},{"id":2,"name":"Quaterly","created_at":"2015-01-14 15:45:06","updated_at":"2015-01-14 15:45:06","pivot":{"catalog_download_id":104,"export_frequency_id":2}},{"id":3,"name":"Monthly","created_at":"2015-01-14 15:45:13","updated_at":"2015-01-14 15:45:13","pivot":{"catalog_download_id":104,"export_frequency_id":3}}]
But I don't want that. I want only the name.
So I did this {{{ $frequencies->name or '' }}} and now nothing come up at all. :(
More details
Here is how I define my relation.
public function export_frequencies(){
return $this->belongsToMany('ExportFrequency','export_frequency_catalog_download','catalog_download_id','export_frequency_id');
}
Can someone tell me what did I do wrong ?
By default, Laravel only selects the two foreign key from the pivot table. To change that add withPivot to your relationship definition:
public function export_frequencies(){
return $this->belongsToMany('Frequency')->withPivot('name');
});
Then you can access it through the pivot object:
#foreach($frequencies as $frequency)
{{ $frequency->pivot->name }}
#endforeach
Edit
I guess I've been confused by your question title. If you just want to display data from ExportFrequency you don't need withPivot and you can simply do this:
#foreach($frequencies as $frequency)
{{ $frequency->name }}
#endforeach

Laravel eloquent relationship and view

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

Resources