Files
crud-laravel/app/Models/Post.php
2024-05-21 14:17:55 +08:00

43 lines
900 B
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Post extends Model
{
use HasFactory;
//columns that are fillable in the table
protected $fillable = [
"content",
'user_id'
];
/**
* Get the user that authored the post.
* If the user is not found, return a default guest author.
*
* @return BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class)->withDefault([
'name' => 'Guest Author',
]);
}
/**
* Get the comments for the post.
*
* @return HasMany
*/
public function comments(): HasMany
{
return $this->hasMany(Comment::class);
}
}