php


Lumen: how to get sub table lists using Eloquent?


I have the following database tables
Towns
Streets
Houses
They have a one to many relationships with each other so Townscan have many Streets and Streets can have multiple Houses on it.
I have the ID of the Town and I just want to get a list of all the Houses in the town from it. So at the moment I'm doing.
//This gives me streets and houses in nested arrays
$towns = Town::with(['streets', 'streets.houses' => function($query) {
$query->where('active', 1)
}])->find($town_id);
//But I only want a list of houses so I do
$houses = $towns->streets->map(function($street) {
return $street->houses;
});
This works but is there a better way of doing this? I feels as though I should just be able to get the list of houses using just eloquent. Having to do some mapping afterwards doesn't seem right.
You are right that lazy loading is not efficient in this case.
You just need the list of houses, I would just build the proper query with joints.
Here is an example:
House::query()
->join('street', 'house.id', 'street.id')
->join('town', 'street.town_id', '=', 'town.id')
->where('town.id', $town_id)
->get();
Tweak as needed.
Sometimes it makes more sense to use query builder over eloquent.

Related Links

PHP parse ini file that includes variables
Supports transactions, row-level locking, and foreign keys errorno 150
Wordpress directing to Posts page (edit.php) instead of saving post
MYSQL - Error Code :- 1241 “Operand should contain 1 column(s) ”
Singleton in constructor
HHVM - imagettftext(): “No such file or directory”
Yii - How to Show Value of Array in CGridView?
same script and software throws exception on windows
Undefined variable in actionblock template
MySQL - How can I/workaround ordering by the result of a group function
Create SEO-friendly URL from php using htaccess
Php - allow spaces in rewriteRule
how to update customer account balance when in INSERT query execute using mysql and php
query does all rows correct except for row 9
WP query both post type and category
Log PHP exceptions in Laravel Log

Categories

HOME
rust
oracle
weblogic12c
qc
concurrency
windows-10
operating-system
embedded-resource
jint
onvif
phpseclib
telegraf
jpa-criteria
spring-security-oauth2
getopenfilename
ex
stimulsoft
xbox360
opam
tableview
el
altium-designer
clipboard
cube
openbr
instructions
argv
apptentive
directfb
cmis
restful-architecture
amazon-kms
cups
cordys-opentext
eclipse-gmf
flowchart
viewcontroller
code-push
jshint
daz3d
section508
directwrite
android-5.0-lollipop
nusoap
sidewaffle
nrf51
wildfly-9
researchkit
yui3
streamwriter
thread-exceptions
jpda
seccomp
spring-mongo
jtag
wolfram-language
declarative
strpos
polymerfire
currency-exchange-rates
jsr363
android-expansion-files
alwayson
org-babel
relational-model
artisan
scala-macros
deque
inequality
berkeley-db-xml
git-reset
accountmanager
assetic
django-settings
strcmp
jacob
insertion
aero
semicolon
xalan
liveconnect
quartz-core
django-apps
3des
3-tier
self-tracking-entities
html-components
subtext
accumulator
ntruencrypt
icon-language
chronic
jvm-bytecode

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App