Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

There is an important difference between fetching a relationship with parentheses "$this->relationship()" or without '$this->relationship".

$this->relationship() return a query builder object where you can append conditions like where, whereHas etc. before executing the query with first(), get() etc.

$this->relationship returns the result of the query directly.

So in you're case you have to do:

return $this->Product_Category[0]->price; 

Because you want the price of the query result and not the price of a query builder object.

Last updated 9 years ago.
0

And because Product_Category is a collection (HasMany Relation), you have to take an item out of the collection first.

0

Thank you thiver.. my bad, i didnt mentioned what is wanna do exactly sorry im very new to laravel can you please help doing the following

im trying to fetch data from table. categorys and products

categorys has following coloumns 

cat_name
cat_description
cat_price

products has following coloumns 

product_name
product_description
category   				//stores category id
product_price

in index.blade.php of product page. i want to dispaly content as follows

**product name          product description           category                   price** 

casio watch             an anolog watch               clock                      33$

Flash light             LED Flah light                lights                     42$
  1. i want to show to which category each product belongs to in category feild
  2. and as for the price,both product and category have price column. i want to display the addition result of the category price and product price. clock (its a category) has price of 24$ and casio watch (its product) 9$ in product listing, it should display 33$, by adding the values.

please help me in doing these. thank you

0

Sign in to participate in this thread!

Eventy

Your banner here too?

rocky5469 rocky5469 Joined 21 Nov 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.