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

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.

Related

How to get data from third table in eloquent relationships eloquent many to many relationship

I am using Eloquent ORM and I have Book model which connect to BookCategory model and BookCategory connect to Category. The l'm problem facing is how to include data from third table in eloquent relationships?
Book
id
name
Category
id
name
type
BookCategory
id
book_id
category_id
Lets say for example you want to get all the books of a certain category:
assuming your pivot table name is Book_Category
in your Category model:
public function books()
{
return $this->belongsToMany('App\Models\Book', 'Book_Category', 'category_id', 'book_id');
}
and you can eager load category books like :
$categories = Category::get()->load('books');
//OR
$categories = Category::with('books')->get();
If I understand you right, you are looking for the pivot attribute. This makes additional columns of the intermediate table available.
https://laravel.com/docs/8.x/eloquent-relationships#retrieving-intermediate-table-columns

foreach the data of relationship

I have two tables media_user and users
media_user table like [ id | user_id | media_id | link ]
I need to bring all user from media_user by relationship, so I created relation in user model:
/**
* that users belong to the media_user table.
*/
public function media()
{
return $this->hasMany('App\Models\Social_Media','user_id','id');
}
And in the view, I did this
#foreach ($user->media as $m)
// my data
#endforeach
I got the empty array when I dump the result as dd($user->media)
What is the problem pls?
The relation looks fine, please check
there are some media items for the user with a correct foreign key.
there is an attribute in the user table called media.
there is no global scope.
the $user is a valid object and belongs to the ORM model
I suggest installing debug bar and view executed query on the media table

laravel has many through piviot table

I have 3 tables
Products
id, name, image
Offers
id,name
Offer_product
id,offer_id,product_id
I am accessing data of product table with pagination using
$list = Product::query()->paginate(6);
now i want the complete record of products with offer name stored in offers table . id of both product and offers are stored in offer_product table where one products can have many offers
In your DB design you have a Many-to-Many relation, which in Laravel is handle by belongsToMany https://laravel.com/docs/5.7/eloquent-relationships#many-to-many.
hasManyThrough is for cascade 1-to-Many -> 1-to-Many case. Let's say an Artist has many Albums and each Album has many Songs, then an Artist has many Songs through Albums. If you need an Artist Songs, then you may use directly hasManyThrough.
In your case your relation in Product.php model should be :
public function offers() {
return $this->belongsToMany(Offer::class, 'Offer_product')->withPivot('id');
}
In Offer.php model :
public function products() {
return $this->belongsToMany(Product::class, 'Offer_product')->withPivot('id');
}
Now if you want all of them with eager loading https://laravel.com/docs/5.7/eloquent-relationships#eager-loading to avoid N(products)+1 calls to database :
$products = Product::with('offers')->get();
foreach ($products as $product) {
echo 'product : '.$product->name.'<br/>';
foreach($product->offers as $offer) {
echo '<br>---- offer : '.$offer->name;
// once you called the relation, then you can access data on pivot table
echo ' - pivot id :'.$offer->pivot->id;
}
}

How to join pivot table in 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.

Laravel5.3: How to implement further relationship in Pivot Table in Many Many Relationship?

So I have this below simple DB structure where I am doing a Many to Many relationship between Users & Roles.
I have managed to do the Many to Many relationships so I can get:
$user->roles() as well as role->users(), however, as you can see I have an additional column in pivot table user_roles i.e. author_id who is the person who granted this role. This is again a one to one relation to a Users table.
My question is how can I establish this relationship in pivot table, so I am able to do:
#foreach($role->users as $user)
{!! $user->firstname !!} role granted by {!! $user->pivot->first_name !!}
#endforeach
something like that..
Looks to me like you need to use the Pivot class to give the pivot table its own model, Pivot extends Model and can have its own relationships.
Laravel Docs: Eloquent Relationships

Resources