Skip to main content

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 Customer model has a polymorphic many-to-many relationship with the Address model. 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 Customer model has a one-to-many relationship with the Comment model. 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 Customer model has a has-many-through relationship with the Payment model through the Order model. 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 Customer model has a one-to-many relationship with the Order model. This means that a customer can have multiple orders.
    public function orders(): HasMany
    {
    return $this->hasMany(Order::class, 'shop_customer_id', 'id');
    }
  • Buyer: The Customer model has a belongs-to relationship with the Buyer model. 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 getActivitylogOptions method returns the default logging options for the model.
    public function getActivitylogOptions(): LogOptions
    {
    return LogOptions::defaults();
    }