main

filamentphp/demo

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

Author.php

TLDR

This file contains the Author class, which is a model class for authors in a blog. It extends the Model class and has a relationship with the Post model.

Classes

Author

The Author class is a model class that represents authors in a blog. It extends the Laravel Model class and uses the HasFactory trait. The class has a property $table which specifies the corresponding database table name as "blog_authors".

Methods

posts()

This method defines a one-to-many relationship with the Post model. It returns a HasMany relationship instance. The relationship is based on the foreign key "blog_author_id" in the posts table.

<?php

namespace App\Models\Blog;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Author extends Model
{
    use HasFactory;

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

    public function posts(): HasMany
    {
        return $this->hasMany(Post::class, 'blog_author_id');
    }
}