Paste

Tung Pham (tungpham42) php Public Mar 03, 2026 03:42 PM
Share
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo; // Import this

class Paste extends Model
{
    protected $fillable = [
        'user_id', 'slug', 'title', 'content', 'syntax',
        'visibility', 'password', 'expires_at'
    ];

    protected function casts(): array
    {
        return [
            'expires_at' => 'datetime',
            'password' => 'hashed',
        ];
    }

    // Add this relationship method
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    // Automatically resolve routes by slug
    public function getRouteKeyName()
    {
        return 'slug';
    }

    // Scope to filter out expired pastes globally
    protected static function booted()
    {
        static::addGlobalScope('active', function (Builder $builder) {
            $builder->where(function ($query) {
                $query->whereNull('expires_at')
                      ->orWhere('expires_at', '>', now());
            });
        });
    }
}