dwains - 1 month ago

Hello, 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 - 1 month ago

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 :-)

dwains - 4 weeks ago

Im getting a error Call to undefined method Illuminate\Database\Query\Builder::default_crm_phone_numbers() with return $this->model->whereHas('default_crm_phone_numbers', function ($query) {

ryanthompson - 3 weeks ago

You probably need to use the relation name not the slug: defaultCrmPhoneNumbers

dwains - 3 weeks ago

return $this->model->whereHas('defaultCrmPhoneNumbers', function ($query) { is also not working

ryanthompson - 3 weeks ago

Again - not working how so. Error? No results? What's the resulting query?

dwains - 3 weeks ago

Its giving me Call to undefined method Illuminate\Database\Query\Builder::defaultCrmPhoneNumbers() the phone numbers has no stream its just a migration with create the table in the database with the slug and the namespace crm

dwains - 3 weeks 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;
            }
        }
    }