Вход Регистрация
Файл: app/Models/Item.php
Строк: 68
<?php

declare(strict_types=1);

namespace 
AppModels;

use 
AppTraitsUploadTrait;
use 
IlluminateDatabaseEloquentCollection;
use 
IlluminateDatabaseEloquentRelationsBelongsTo;
use 
IlluminateDatabaseEloquentRelationsMorphMany;
use 
IlluminateSupportHtmlString;

/**
 * Class Item
 *
 * @property int id
 * @property int board_id
 * @property string title
 * @property string text
 * @property int user_id
 * @property int price
 * @property string phone
 * @property int created_at
 * @property int updated_at
 * @property int expires_at
 * @property Board category
 * @property Collection files
 */
class Item extends BaseModel
{
    use 
UploadTrait;

    public const 
BOARD_PAGINATE 10;

    
/**
     * Indicates if the model should be timestamped.
     */
    
public $timestamps false;

    
/**
     * The attributes that aren't mass assignable.
     */
    
protected $guarded = [];

    
/**
     * Директория загрузки файлов
     */
    
public string $uploadPath '/uploads/boards';

    
/**
     * Morph name
     */
    
public static string $morphName 'items';

    
/**
     * Возвращает категорию объявлений
     */
    
public function category(): BelongsTo
    
{
        return 
$this->belongsTo(Board::class, 'board_id')->withDefault();
    }

    
/**
     * Возвращает загруженные файлы
     */
    
public function files(): MorphMany
    
{
        return 
$this->morphMany(File::class, 'relate');
    }

    
/**
     * Возвращает путь к первому файлу
     *
     * @return HtmlString|null имя файла
     */
    
public function getFirstImage(): ?HtmlString
    
{
        
$image $this->files->first();

        
$path $image->hash ?? null;

        return 
resizeImage($path, ['alt' => $this->title'class' => 'img-fluid']);
    }

    
/**
     * Возвращает сокращенный текст объявления
     */
    
public function shortText(int $words 50): HtmlString
    
{
        if (
wordCount($this->text) > $words) {
            
$this->text bbCodeTruncate($this->text$words);
        } else {
            
$this->text bbCode($this->text);
        }

        return new 
HtmlString($this->text);
    }

    
/**
     * Удаление объявления и загруженных файлов
     */
    
public function delete(): ?bool
    
{
        
$this->files->each(static function (File $file) {
            
$file->delete();
        });

        return 
parent::delete();
    }
}
Онлайн: 3
Реклама