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

42 lines
882 B
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Comment extends Model
{
//columns that are fillable in the table
protected $fillable = [
"content",
'user_id',
'post_id'
];
use HasFactory;
/**
* Get the post that the comment belongs to.
*
* @return BelongsTo
*/
public function post(): BelongsTo
{
return $this->belongsTo(Post::class);
}
/**
* Get the user that authored the comment.
* 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',
]);
}
}