php


Laravel 5.4 eloquent one to many relationship data fetching


I have two tables. 1.products 2.product_images.
Field of products Table: id,product_name,created_at,updated_at
Field of product_images Table: id,product_id,product_image,created_at,updated_at
products table has OnetoMany relationship with product_images table.
So in the Product model I have defined :
public function productImage()
{
return $this->hasMany(Product_image::class,'product_id');
}
Now I want to to get all the data of product id 1 from both tables.
So I have this code below:
App\Product::find(1)->productImage
Result Set is:
Illuminate\Database\Eloquent\Collection {#689
all: [
App\Product_image {#679
id: 1,
product_id: 1,
product_image: "1494787942_download (1).jpg",
created_at: "2017-05-14 18:52:22",
updated_at: "2017-05-14 18:52:22",
},
],
}
But I am not getting the product_name. What will be the solution?
Thanks.
productImage doesn't have a product_name property, but product does. When you show the results of App\Product::find(1)->productImage; you can't expect it to show a product_name property.
If you wanted to see the product and it's attached image, you would do this:
$product = \App\Product::with(["productImage"])->find(1);
Then, doing a dd($product) would show you the product (including the product_name attribute) and the productImage as an attribute that returns a Collection of productImage records.
Keep in mind that have a function named productImage that returns multiple productImage records is misleading; naming convention would suggest productImages().

Related Links

PHP API Frameworks for beginner [closed]
Can't access sub directory on server
HTTP URL Encoded Request failing
check if the current post is one of the recent 5 post
encoding in doctrine or twig have problems
Embedded document for Signing -Docu sign REST API
Need to reset the file permission in plesk
Extract URL from curl response
How can i include files to use class in other included files?
How do I get rid of the duplicates in this double mysql join?
Placing database on one host and PHP files on another [closed]
PHP Array not being delivered
Propel using a single query to get a result and change the order
Database relationship between two databases
Google AppEngine communicating with Compute Engine
Using bind for form with lot of inputs (PHP)

Categories

HOME
openshift
ionic2
firebase
protocol-buffers
macos-sierra
softlayer
plaid
braintree
gallery
google-sheets-query
onvif
cisco
port
elastic-load-balancer
csrf
smooks
grocery-crud
cuba-platform
c#-7.0
mixpanel
salt-cloud
spam
unity-container
activeadmin
acl
web-push
router
window-managers
bobo-browse.net
adapter
bitbake
monitor
phpstorm-2016.1
sca
wdk
aurelia-cli
ms-dos
cloudera-sentry
elasticsearch-aggregation
git-tfs
code-push
windows-media-player
cloudwatch
spring.net
subforms
http-basic-authentication
1010
mpmovieplayercontroller
d3v4
researchkit
okio
s-function
ajaxmin
xcopy
verisign
ajax4jsf
boost-proto
calcite
pythonxy
wso2ml
fantom
vdm-sl
swagger-maven-plugin
karma-coverage
phpredis
dbmigrate
scala-swing
android-audiomanager
jcheckbox
hp-idol-ondemand
multivariate-testing
aho-corasick
windows-messages
gpu-programming
cloo
sql-view
ember-table
ax
linkbutton
system-requirements
gitx
memory-pool
expression-web
play2-mini
firefly-mv
uipasteboard
mongrel2
sscli
xap
motif
account-management

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile