main

filamentphp/demo

Last updated at: 29/12/2023 09:42

Customer.php

TLDR

This file defines the Customer model class in the App\Models\Shop namespace. The Customer model extends the Laravel Model class and uses the HasFactory and SoftDeletes traits. It has three methods: addresses, comments, and payments, which define the relationships with the Address, Comment, and Payment models respectively.

Methods

addresses

This method returns a MorphToMany relationship with the Address model. It allows a customer to have multiple addresses.

comments

This method returns a HasMany relationship with the Comment model. It represents the comments made by the customer.

payments

This method returns a HasManyThrough relationship with the Payment model, using the Order model as an intermediate table. It represents the payments made by the customer through their orders.

Classes

No classes defined in this file.

<?php

namespace App\Models\Shop;

use App\Models\Address;
use App\Models\Comment;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Customer extends Model
{
    use HasFactory;
    use SoftDeletes;

    /**
     * @var string
     */
    protected $table = 'shop_customers';

    /**
     * @var array<string, string>
     */
    protected $casts = [
        'birthday' => 'date',
    ];

    public function addresses(): MorphToMany
    {
        return $this->morphToMany(Address::class, 'addressable');
    }

    public function comments(): HasMany
    {
        return $this->hasMany(Comment::class);
    }

    public function payments(): HasManyThrough
    {
        return $this->hasManyThrough(Payment::class, Order::class, 'shop_customer_id');
    }
}