main

filamentphp/demo

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

Comment.php

TLDR

This file defines the Comment model, which represents comments in the application. It has two methods:

  • customer(): BelongsTo method returns the associated customer for the comment.
  • commentable(): MorphTo method returns the polymorphic relationship to the commentable model.

Methods (if applicable)

customer(): BelongsTo

This method returns the associated customer for the comment. It defines a BelongsTo relationship between the Comment model and the Customer model.

commentable(): MorphTo

This method returns the polymorphic relationship to the commentable model. It defines a MorphTo relationship between the Comment model and other models that can be commentable.

Classes (if applicable)

Comment

This class represents comments in the application. It extends the Model class and uses the HasFactory trait. The Comment model has the following properties:

  • $table: Specifies the database table name for the model.
  • $guarded: Defines the attributes that are not mass assignable.
  • $casts: Specifies the attribute casting for specific data types.
<?php

namespace App\Models;

use App\Models\Shop\Customer;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class Comment extends Model
{
    use HasFactory;

    protected $table = 'comments';

    protected $guarded = [];

    protected $casts = [
        'is_visible' => 'boolean',
    ];

    public function customer(): BelongsTo
    {
        return $this->belongsTo(Customer::class);
    }

    public function commentable(): MorphTo
    {
        return $this->morphTo();
    }
}