Customer
Definition and Role:
The Customer model represents a customer within the application. It is used to manage and store information related to customers who make purchases from the vendor's (Buyer) shop. This model is essential for handling customer details, addresses, comments, orders, and payments.
Relationships:
- Addresses: The
Customermodel has a polymorphic many-to-many relationship with theAddressmodel. This means that a customer can have multiple addresses, and an address can be associated with multiple customers, but that does not mean you should actually associate a single address with more than one customer./** @return MorphToMany<Address> */
public function addresses(): MorphToMany
{
return $this->morphToMany(Address::class, 'addressable');
} - Comments: The
Customermodel has a one-to-many relationship with theCommentmodel. This means that a customer can have multiple comments associated with them. Currently the admin can create a comment on a product and link it to a specific customer/** @return HasMany<Comment> */
public function comments(): HasMany
{
return $this->hasMany(Comment::class);
} - Payments: The
Customermodel has a has-many-through relationship with thePaymentmodel through theOrdermodel. This means that a customer can have multiple payments associated with their orders./** @return HasManyThrough<Payment> */
public function payments(): HasManyThrough
{
return $this->hasManyThrough(Payment::class, Order::class, 'shop_customer_id');
} - Orders: The
Customermodel has a one-to-many relationship with theOrdermodel. This means that a customer can have multiple orders.public function orders(): HasMany
{
return $this->hasMany(Order::class, 'shop_customer_id', 'id');
} - Buyer: The
Customermodel has a belongs-to relationship with theBuyermodel. This means that a customer is associated with a specific buyer (vendor).public function buyer(): BelongsTo
{
return $this->belongsTo(Buyer::class);
}
Traits:
HasFactory: Provides factory methods for creating model instances.SoftDeletes: Enables soft deleting for the model, allowing records to be "deleted" without being permanently removed from the database.LogsActivity: Enables activity logging for the model.
Attributes:
table: Specifies the database table name associated with the model.protected $table = 'shop_customers';casts: Defines the data types of the model's attributes.protected $casts = [
'birthday' => 'date',
];
Activity Log Options:
- The
getActivitylogOptionsmethod returns the default logging options for the model.public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults();
}