Brand.php
TLDR
This file defines the Brand
class, which represents a brand in a shop. It extends the Model
class and implements the HasMedia
interface, allowing it to have media attachments. The Brand
class has a table name of shop_brands
in the database and has a is_visible
attribute that is casted to a boolean.
Classes
Brand
The Brand
class represents a brand in a shop. It extends the Model
class and implements the HasMedia
interface. This class has the following methods:
-
addresses(): MorphToMany
: This method returns aMorphToMany
relation, defining the relationship betweenBrand
andAddress
. This means that a brand can have multiple addresses associated with it. -
products(): HasMany
: This method returns aHasMany
relation, defining the relationship betweenBrand
andProduct
. This means that a brand can have multiple products associated with it.
<?php
namespace App\Models\Shop;
use App\Models\Address;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class Brand extends Model implements HasMedia
{
use HasFactory;
use InteractsWithMedia;
/**
* @var string
*/
protected $table = 'shop_brands';
/**
* @var array<string, string>
*/
protected $casts = [
'is_visible' => 'boolean',
];
public function addresses(): MorphToMany
{
return $this->morphToMany(Address::class, 'addressable', 'addressables');
}
public function products(): HasMany
{
return $this->hasMany(Product::class, 'shop_brand_id');
}
}