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