Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent

Hello again,

I have an Eloquent model set up that contains a foreign key to a simple (id, name) lookup table, very similar to the category_id described in this thread here. I also have set up the belongsTo() relation, so I can reference the value like $model->category->type.

I was wondering if there was a more "user friendly" way to rig my models so this type of look-up relation is easier to interact with in the PHP code. For example, I'd like to be able to do things without the nested dereference ($model->type) and perform get/set operations with the full type name instead of the ID ( if ($model->type == 'foo') or $model->type = 'bar' rather than if ($model->category->type == 'foo') and $model->category_id = Category::where('type', 'bar')->get()->first()->id).

Any thoughts? Thanks!

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

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.

© 2025 Laravel.io - All rights reserved.