Mar 10, 2020
0
Level 2
Use Relationship Value as Property
I have a one to many relation. The One is a Users Table . and the Many have columns: Id, user_id, value_key, value I'm able to retrieve the relation with no issues but i would like to make it smarter. I would like to make the "value_key" column into properties of the relation. $users->relation->value_key , so it returns the "value" column.
So pretty much assign the value keys as properties for my relation
Atm, my relation returns multiple rows and I have to iterate through every single one to see if a specific value_key exists. I think this will not be efficient if i have to this on my index pagination View.
Any tips how i can achieve this? Thanks!
This is my eloquent call
global $deptModuleId;
$deptModuleId = $moduleId;
$query = SELF::
select('taxonomy_meta.*', 'taxonomy.*')
->with(['taxChildren' => function($query){
global $deptModuleId;
$query->select('taxonomy_meta.*', 'taxonomy.*')
->where([
['module', '=', $deptModuleId ],
['meta_key', '=', 'position'],
])
->leftJoin('taxonomy_meta', 'taxonomy.id', '=', 'taxonomy_meta.tax_id')
->orderByRaw('LENGTH(taxonomy_meta.meta_value)', 'asc')
->orderBy('taxonomy_meta.meta_value', 'asc');
}])
->leftJoin('taxonomy_meta', 'taxonomy.id', '=', 'taxonomy_meta.tax_id')
->where([
['module', '=', $moduleId],
['meta_key', '=', 'position'],
])
->whereNull('parent')
->orderByRaw('LENGTH(taxonomy_meta.meta_value)', 'asc')
->orderBy('taxonomy_meta.meta_value', 'asc');
if($withTax == true){
$query = $query->with('taxMeta', 'taxChildren.taxMeta');
}
$departments = $query->get();
return $departments;
Please or to participate in this conversation.