This post may scream ignorance (Still delving into the plugin world). I'm coming from a background using mostly laravel for PHP application development and building a few plugins now on Craft has felt a bit verbose to access related objects. Is there any "nice" way to allow something like the following in Craft?

$phone = User::find(1)->phone;

or in terms of craft plugins, just include related data in the model itself instead of having dozens of findXbyId methods littering the code?

related: populateModel usage with eager loading related records

Any generic solutions?


1 Answer 1


Not 100% sure I'm following what you're looking for, but if you have a UserRecord defined with a phone relation, then something like this should do what you want:

$phone = UserRecord::model()->with('phone')->findById(1)->phone;
  • I suppose that will do, I was trying to find a way to somewhat automate it but it might just be overkill. What would be the syntax to call an extra relation? $phone = UserRecord::model()->with('phone, age')->findById(1)->phone; Apr 30, 2015 at 4:27
  • Got it. $phone = UserRecord::model()->with('phone', 'age')->findById(1)->phone; Apr 30, 2015 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.