Category.php
TLDR
This file defines the Category
class, which is a model representing a blog category in the application.
Methods
posts
This method defines a relationship between the Category
model and the Post
model. It specifies that a category can have multiple posts. The method returns an instance of the HasMany
class.
Classes
Class Category
This class represents a blog category in the application. It extends the Model
class provided by the Laravel framework and uses the HasFactory
trait.
The Category
class has the following properties:
-
$table
(string): Specifies the database table name for the model (blog_categories
). -
$casts
(array): Specifies the data types of certain attributes of the model.
The Category
class also defines the following method:
-
posts()
: Defines a relationship between theCategory
model and thePost
model, indicating that a category can have multiple posts.
<?php
namespace App\Models\Blog;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Category extends Model
{
use HasFactory;
/**
* @var string
*/
protected $table = 'blog_categories';
/**
* @var array<string, string>
*/
protected $casts = [
'is_visible' => 'boolean',
];
public function posts(): HasMany
{
return $this->hasMany(Post::class, 'blog_category_id');
}
}