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 or htaccess - allow external server to check if file exists
How to set values in the select dropdown box?
Getting Controller does not exist after moving my controller to sub folder in laravel 5
Mysqli Count Two Data Type in one query
Invalid argument key “nameOfTheArgument” found symfony3 (Extending an abstract service)
Codecademy Functions, Part I PHP [duplicate]
Failed opening required '../vendor/autoload.php'
Compare between Two dates in php [duplicate]
Altering another document in Doctrine ODM PreUpdate event
FB access token is empty without any FB exception thrown from PHP SDK v5
Stream a PDF file in Laravel 5.2
BroadcastOn not firing in laravel 5.3?
How to execute a Symfony command as cron job?
use laravel Authentication in pure php scripts
show the data from complex json data in php
.htacces redirect subdomain to folder

Categories

HOME
matrix
blast
android-fragments
livecode
website
odata
typeerror
twitter-fabric
openfoam
facebook-android-sdk
haxe
javafx-8
lda
xades4j
tiff
spring-amqp
mailmerge
rest-assured
susy-sass
deepfreeze
raml
weex
bsd
qa
pdf.js
vuforia
web-push
introspection
beego
fstar
boolean-logic
social-networking
nic
riotjs
cucumber-junit
xmlunit
nest
directfb
dreamfactory
pdfminer
nsuserdefaults
django-crispy-forms
microsoft-ui-automation
web-inspector
jshint
rightnow-crm
return-type
password-hash
ilrepack
visual-format-language
sequence-diagram
word-2016
rustdoc
portal
ndk-build
livecycle
spring-mongo
oci
python-cffi
bayesglm
roxygen2
google-gdk
ipod
subfolder
sablecc
lifetime
nidaqmx
otl
jini
sqlbindparameter
jomsocial
profiles
django-sites
grails-2.3
iirf
insertonsubmit
codahale-metrics
django-facebook
handshake
arrow
voice-recording
kobold2d
liveconnect
wcf-callbacks
django-apps
haiku
dip
mygeneration
ninject-interception
reliability
android-input-method
getimagesize

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