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!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community