fanatic/app/Traits/Categorizable.php
Marley Rae c1996a415f no
2022-04-23 19:53:35 -07:00

31 lines
601 B
PHP

<?php
namespace App\Traits;
use App\Models\Category;
trait Categorizable
{
/* ---------------------------------------------------------------------------- relationship ---- */
public function categories()
{
return $this->morphToMany(Category::class, 'categorizable');
}
/* -------------------------------------------------------------------------- listCategories ---- */
public function listCategories() : string
{
$cats = $this->categories;
$str = '';
if ($cats->isNotEmpty()) {
foreach ($cats as $cat) {
$str .= "$cat->name, ";
}
}
return rtrim($str, ', ');
}
}