hasMany(CustomerInfo::class); } /** * Get the CustomerInfo from a specific brand * or create a New one if $firstOrNewOrFail is 1 * throw model not found exception if firstOrNewOrFail = 2 * * @param Brand $brand * @param int $firstOrNewOrFail * @return \Illuminate\Database\Eloquent\Builder */ public function info(Brand $brand, $firstOrNewOrFail = self::FIRST_OR_NULL): ?CustomerInfo { if (isset($this->relations['infos'])) { /** @var \App\Models\CustomerInfo $info */ foreach ($this->relations['infos'] as $info) { if ($info->brand_id === $brand->id) { return $info; } } } $query = CustomerInfo::whereHas('brand', function (Builder $query) use ($brand) { $query->where('id', $brand->id); })->whereHas('customer', function (Builder $query) { $query->where('id', $this->id); }); if ($firstOrNewOrFail === self::FIRST_OR_NEW) { return $query->firstOrNew(); } if ($firstOrNewOrFail === self::FIRST_OR_FAIL) { return $query->firstOrFail(); } return $query->first(); } /** * CustomerLogin hasMany relation * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function logins() { return $this->hasMany(CustomerLogin::class); } /** * Get the CustomerLogin from a specific brand * or create a New one if $firstOrNewOrFail is 1 * throw model not found exception if firstOrNewOrFail = 2 * * @param Brand $brand * @param int $firstOrNewOrFail * @return \Illuminate\Database\Eloquent\Builder */ public function login(Brand $brand, $firstOrNewOrFail = self::FIRST_OR_NULL): ?CustomerLogin { if (isset($this->relations['logins'])) { /** @var \App\Models\CustomerLogin $login */ foreach ($this->relations['logins'] as $login) { if ($login->brand_id === $brand->id) { return $login; } } } $query = CustomerLogin::whereHas('brand', function (Builder $query) use ($brand) { $query->where('id', $brand->id); })->whereHas('customer', function (Builder $query) use ($brand) { $query->where('id', $this->id); }); if ($firstOrNewOrFail === self::FIRST_OR_NEW) { return $query->firstOrNew(); } if ($firstOrNewOrFail === self::FIRST_OR_FAIL) { return $query->firstOrFail(); } return $query->first(); } /** * Load login and brand for this customer * * @param Brand $brand * @return self */ public function loadBrandData(Brand $brand, bool $orFail = false): self { $this->load([ 'logins' => function ($q) use ($brand) { $q->where('brand_id', $brand->id); }, 'infos' => function ($q) use ($brand) { $q->where('brand_id', $brand->id); } ]); if ($orFail and !isset($this->relations['logins'][0])) { throw (new ModelNotFoundException())->setModel(CustomerLogin::class); } if ($orFail and !isset($this->relations['infos'][0])) { throw (new ModelNotFoundException())->setModel(CustomerInfo::class); } return $this; } /** * CustomerOptInHistory hasMany relation * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function optInHistory() { return $this->hasMany(CustomerOptInHistory::class); } /** * Customer Brand belongsToMany relation * * @return \Illuminate\Database\Eloquent\Relations\belongsToMany */ public function brands() { return $this->belongsToMany(Brand::class, CustomerInfo::class); } /** * Answers belongsToMany relation * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function answers() { return $this->belongsToMany(Answer::class, 'customer_answer'); } public function setOptIn(Brand $brand, bool $optIn) { // Ajout dans la table d'historique des opt-in de la nouvelle valeur $optInHistory = new CustomerOptInHistory(); $optInHistory->customer_id = $this->id; $optInHistory->brand_id = $brand->id; $optInHistory->new_value = $optIn; $optInHistory->saveOrFail(); } /** * Get All answers given by this customer * for a specific service * * @param Service $service * @return Collection */ public function getAnswers(Service $service): Collection { return Answer::whereHas('customers', function ($q) { $q->whereId($this->id); })->whereHas('question', function ($q) use ($service) { $q->whereHas('brand', function ($q) use ($service) { $q->whereId($service->brand->id); }); })->get(); } }