Find a entry by a repeater field
Created 3 years ago by dwainsHello, I'm working on a new module with profiles.
-
I created a migration file with:
protected $fields = [ 'phone_number' => [ 'type' => 'anomaly.field_type.text', 'config' => [ ] ], ]; protected $stream = [ 'slug' => 'phone_numbers', 'sortable' => true, 'translatable' => false, ]; protected $assignments = [ 'phone_number', ];
-
I created a new stream called profiles and it has in the migration:
protected $fields = [ 'phone_numbers' => [ 'type' => 'anomaly.field_type.repeater', 'config' => [ 'related' => 'modulename.phone_numbers', ], ],
Now in the Http Controller I want to find a profile by a phone_number.
I tried in the Profile stream to create a function in the ProfileRepository
file:
public function findByPhoneNumber($phone_number)
{
return $this->model->whereHas('phone_numbers', function ($query) {
$query->where('phone_number', $phone_number);
});
}
And in the Http controller
public function info(ProfileRepositoryInterface $profiles, $phone_number)
{
$profile = $profiles->findByPhoneNumber($phone_number);
dd($profile);
But that doesnt work. How can I make this work?
ryanthompson
—
3 years ago
You probably need to use the relation
name not the slug: defaultCrmPhoneNumbers
ryanthompson
—
3 years ago
Again - not working how so. Error? No results? What's the resulting query?
dwains
—
3 years ago
I created this function in my ProfileRepository and it seems to work
public function findByPhoneNumber($phone_number)
{
$phoneNumberCheck = DB::table('crm_phone_numbers')
->where('phone_number', $phone_number)
->join('crm_profile_phone_numbers', 'crm_phone_numbers.id', '=', 'crm_profile_phone_numbers.related_id')
->first();
if($phoneNumberCheck){
$profile = $this->model->find($phoneNumberCheck->entry_id);
if($profile){
return $profile;
}
}
}
What's the resulting query? What do you mean it "doesn't work"? Does it bomb out? Or does it simply not return the results?
Another way to approach it would be to find the phone numbers in the repeater stream and then use the entry relation to get your profile 😄