Some times, Developers used this package need to override default model or extend it.
So used hardcoded model name in Morph relations will break.
Here is example in my case:
File : app/Models/Post.php
class Post extends \TomatoPHP\FilamentCms\Models\Post implements HasMedia
{
use InteractsWithMedia;
protected $with = ['postMeta', 'media', 'categories', 'comments', 'author', 'tags'];
public function next()
{
return static::where('id', '>', $this->id)->where('is_published', true)->where('type', '=', $this->type)->orderBy('id', 'asc')->first();
}
public function previous()
{
return static::where('id', '<', $this->id)->where('is_published', true)->where('type', '=', $this->type)->orderBy('id', 'desc')->first();
}
// just demo extended case
public function scopePublished(Builder $query): Builder
{
return $query->whereNotNull('published_at');
}
}
Then when Morph query builder load media, comments query will like this one:
select count(*) as aggregate from `comments` where `comments`.`content_type` = 'App\\Models\\Post' and `comments`.`content_id` = 10 and `comments`.`content_id` is not null
But actual value in DB is 'TomatoPHP\\FilamentCms\\Models\\Post'.
Currently. I must used workaround hack: reupdate value when post created and update. But when comment added. Post is remain unchanged
namespace App\Listeners;
use TomatoPHP\FilamentCms\Events\PostCreated;
use TomatoPHP\FilamentCms\Events\PostUpdated;
use Illuminate\Support\Facades\DB;
class UpdatePostMediaModelType
{
/**
* Handle the event.
*/
public function handle(PostCreated|PostUpdated $event): void
{
// Update media model_type for the specific post
DB::table('media')
->where('model_type', 'TomatoPHP\FilamentCms\Models\Post')
// ->where('model_id', $event->post->id)
->update(['model_type' => 'App\Models\Post']);
DB::table('comments')
->where('model_type', 'TomatoPHP\FilamentCms\Models\Post')
// ->where('model_id', $event->post->id)
->update(['model_type' => 'App\Models\Post']);
}
}
Some times, Developers used this package need to override default model or extend it.
So used hardcoded model name in
Morph
relations will break.Here is example in my case:
File :
app/Models/Post.php
Then when
Morph
query builder load media, comments query will like this one:But actual value in DB is
'TomatoPHP\\FilamentCms\\Models\\Post'
.Currently. I must used workaround hack: reupdate value when post created and update. But when comment added. Post is remain unchanged