File: //home/armgroup/libs/app/Models/Favorite.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Favorite extends Model
{
use HasFactory;
protected $guarded = ['id'];
public const TYPE = [
[
'name' => 'محصولات',
'show-name' => 'محصول',
'title' => 'name',
'table' => 'products',
'className' => Product::class,
],
[
'name' => 'بلاگ',
'show-name' => 'مطلب',
'title' => 'title',
'table' => 'articles',
'className' => Article::class,
],
];
public function favoritable()
{
return $this->morphTo();
}
public function user()
{
return $this->belongsTo(User::class);
}
public function getFavoriteType()
{
return collect(self::TYPE)->firstWhere('className', $this->favoritable_type);
}
public static function getTypeByTable($table)
{
return collect(self::TYPE)->firstWhere('table', $table);
}
}