App\Ninja\Repositories\ClientRepository::save PHP Method

save() public method

public save ( $data, $client = null )
    public function save($data, $client = null)
    {
        $publicId = isset($data['public_id']) ? $data['public_id'] : false;
        if ($client) {
            // do nothing
        } elseif (!$publicId || $publicId == '-1') {
            $client = Client::createNew();
        } else {
            $client = Client::scope($publicId)->with('contacts')->firstOrFail();
        }
        if ($client->is_deleted) {
            return $client;
        }
        // convert currency code to id
        if (isset($data['currency_code'])) {
            $currencyCode = strtolower($data['currency_code']);
            $currency = Cache::get('currencies')->filter(function ($item) use($currencyCode) {
                return strtolower($item->code) == $currencyCode;
            })->first();
            if ($currency) {
                $data['currency_id'] = $currency->id;
            }
        }
        $client->fill($data);
        $client->save();
        /*
        if ( ! isset($data['contact']) && ! isset($data['contacts'])) {
            return $client;
        }
        */
        $first = true;
        $contacts = isset($data['contact']) ? [$data['contact']] : $data['contacts'];
        $contactIds = [];
        // If the primary is set ensure it's listed first
        usort($contacts, function ($left, $right) {
            if (isset($right['is_primary']) && isset($left['is_primary'])) {
                return $right['is_primary'] - $left['is_primary'];
            } else {
                return 0;
            }
        });
        foreach ($contacts as $contact) {
            $contact = $client->addContact($contact, $first);
            $contactIds[] = $contact->public_id;
            $first = false;
        }
        if (!$client->wasRecentlyCreated) {
            foreach ($client->contacts as $contact) {
                if (!in_array($contact->public_id, $contactIds)) {
                    $contact->delete();
                }
            }
        }
        if (!$publicId || $publicId == '-1') {
            event(new ClientWasCreated($client));
        } else {
            event(new ClientWasUpdated($client));
        }
        return $client;
    }

Usage Example

 /**
  * @param $data
  * @param null $client
  * @return mixed|null
  */
 public function save($data, $client = null)
 {
     if (Auth::user()->account->isNinjaAccount() && isset($data['plan'])) {
         $this->ninjaRepo->updatePlanDetails($data['public_id'], $data);
     }
     return $this->clientRepo->save($data, $client);
 }
All Usage Examples Of App\Ninja\Repositories\ClientRepository::save