Category.php
TLDR
This file contains the Category
model class, which represents a category in a shop. It extends the Laravel Model
class and implements the HasMedia
interface from the Spatie Media Library package.
Classes
Category
The Category
model class represents a category in a shop. It extends the Laravel Model
class and implements the HasMedia
interface from the Spatie Media Library package. This class has the following methods:
-
children(): HasMany
: Returns a relationship defining that a category can have many children categories. -
parent(): BelongsTo
: Returns a relationship defining that a category belongs to a parent category. -
products(): BelongsToMany
: Returns a relationship defining that a category can have many products.
<?php
namespace App\Models\Shop;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class Category extends Model implements HasMedia
{
use HasFactory;
use InteractsWithMedia;
/**
* @var string
*/
protected $table = 'shop_categories';
/**
* @var array<string, string>
*/
protected $casts = [
'is_visible' => 'boolean',
];
public function children(): HasMany
{
return $this->hasMany(Category::class, 'parent_id');
}
public function parent(): BelongsTo
{
return $this->belongsTo(Category::class, 'parent_id');
}
public function products(): BelongsToMany
{
return $this->belongsToMany(Product::class, 'shop_category_product', 'shop_category_id', 'shop_product_id');
}
}