query model by custom fields

Laraship QuestionsCategory: Technicalquery model by custom fields
matt.jones asked 5 years ago
i added custom_field named "is_model" related with ECommerceProduct and want to make a query function in Shop::class that filters products using this custom field like $product->where('is_model',1); i've tried by many ways and only one solution worked for me to collect all products and filter them through foreach loop , i need optimal solution urgently please.
1 Answers
laraship Staff answered 5 years ago


if you check CustomFieldTrait there is a relation that can be used to join the models with their custom fields values


* Return custom field relation for specified field name.


* @param $fieldName

* @return mixed


public function customFieldRelation($fieldName)


return $this->morphOne(CustomField::class, 'parent', 'parent_type')

->where('field_name', $fieldName);


ao I assume you can use it with Model queries.