Query using Eloquent multiple fields

Asked

Viewed 55 times

0

all right?

I would like a help from you to put together a more "elegant" consultation at Laravel. I currently have the following scenario.

I need to check if a client is already registered in my database. For this I can use up to 3 conditions, being by: - email - landline - cellular

Only that these 3 fields often do not come filled in, ie it may be that the client only reported field, or even 2 fields..

But then it gets complicated. For example, if I have only 1 field is easy, just use "Where", but when I have two fields I need to use "orWhere"...

Well, today I made a code that is WORKING, however, I found it extremely ugly... I wonder how I could put together more elegant this.

$customer = Customer::select('*');

        $secondFilter = false;
        $filter = false;
        if (!empty($this->lead['email'])) {
            $secondFilter = true;
            $filter = true;
            $customer = $customer->where('email', $this->lead['email']);
        }

        if (!empty($this->lead['cpf_cnpj'])) {
            $filter = true;
            if ($secondFilter) {
                $customer = $customer->orWhere('cpf_cnpj', $this->lead['cpf_cnpj']);
            } else {
                $customer = $customer->where('cpf_cnpj', $this->lead['cpf_cnpj']);
            }
            $secondFilter = true;
        }

        if (!empty($this->lead['phone'])) {
            $filter = true;
            if ($secondFilter) {
                $customer = $customer->where('phone', $this->lead['phone']);
            } else {
                $customer = $customer->orWhere('phone', $this->lead['phone']);
            }
            $secondFilter = true;
        }

        if (!empty($this->lead['mobile'])) {
            $filter = true;
            if ($secondFilter) {
                $customer = $customer->where('mobile', $this->lead['mobile']);
            } else {
                $customer = $customer->orWhere('mobile', $this->lead['mobile']);
            }
        }

        $customer = $filter ? $customer->first() : false;

        $newRegister = false;
        $this->lead['action_lead'] = 'UPDATE';
        if (!$customer) {
            $newRegister = true;
            $customer = new Customer;
            $this->lead['action_lead'] = 'NEW';
        }

        if ($this->lead['company_id']) {
            $customer->company_id = $this->lead['company_id'];
        }

        if ($this->lead['name'] && $newRegister) {
            $customer->name = $this->lead['name'];
        }

        //update name for differnte lead calltracking
        if ($this->lead['name']) {
            $customer->name = $this->lead['name'];
        }

        if (isset($this->lead['city']) && !empty($this->lead['city'])) {
            $customer->city = $this->lead['city'];
        }

        if (isset($this->lead['state']) && !empty($this->lead['state'])) {
            $customer->state = $this->lead['state'];
        }

        if ($this->lead['email'] && !empty($this->lead['email'])) {
            $customer->email = $this->lead['email'];
        }

        if ($this->lead['cpf_cnpj'] && !empty($this->lead['cpf_cnpj'])) {
            $customer->cpf_cnpj = $this->lead['cpf_cnpj'];
        }

        if ($this->lead['phone'] && !empty($this->lead['phone'])) {
            $customer->phone = $this->lead['phone'];
        }

        if (isset($this->lead['mobile']) && !empty($this->lead['mobile'])) {
            $customer->mobile = $this->lead['mobile'];
        }

        $customer->updated_at = date('Y-m-d H:i:s');
        $customer->save();

        $this->customer = $customer;

1 answer

0

I understood well the $filter and $secondFilter...

$customer = new Customer();

// Se tem campo email busca pelo cliente com o email
if (isset($this->lead['email'])) {
    $customer->where('email', $this->lead['email']);
}

// Se tem campo cpf/cnpj busca pelo cliente com o cpf/cnpj
if (isset($this->lead['cpf_cnpj'])) {
    $customer->where('cpf_cnpj', $this->lead['cpf_cnpj']);
}

// Se tem campo telefone busca pelo cliente com o telefone
if (isset($this->lead['phone'])) {
    $customer->where('phone', $this->lead['phone']);
}

// Se tem campo celular busca pelo cliente com o celular
if (isset($this->lead['mobile'])) {
    $customer->where('mobile', $this->lead['mobile']);
}

/**
 * Tenta acessar o primeiro cliente com as
 * informações preenchidas, senão, cria um novo.
 */
$customer = $customer->firstOrCreate($this->lead);

if the columns in db are equal to the field names it is NOT necessary to assign the values manually:

$customer->name = $this->lead['name'];

the idea is +- this, modify according to your need.

see documentation for more information

  • The Filter and Secondfilter variable helps me because I need to use the OR condition, that is, the way that Cvoce set up all the conditions need to be the same, however, I need to use the OR condition if any is true. And as I mentioned at the beginning, there may be situations where only CPF_CNPJ is filled, or Email, or it may have the 2 values, anyway... using only Where does not solve the problem because one or another condition may have data.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.