Need Help - Touches Relationships Laravel scout & meilisearch
seifrached opened this issue · comments
I'm creating a bookmark system for users by 3 models who
User can make Many records
Record Bookmarked by her owner and others Many Users
Bookmark belongsTo Record and User
Models relationship
Record.php
class Record extends Model
{
use HasFactory, Searchable, SoftDeletes;
public $table = 'records';
/**
* All of the relationships to be touched.
*
* @var array
*/
protected $touches = ['user'];
public function user(){
return $this->belongsTo(User::class);
}
public function bookmarks(){
return $this->hasMany(Bookmark::class,'record_id')->withTrashed();
}
}
User.php
public function bookmarks()
{
return $this->hasMany(Bookmark::class);
}
public function records()
{
return $this->hasMany(Record::class);
}
Bookmark.php
class Bookmark extends Model
{
use HasFactory, Searchable, SoftDeletes;
public $table = 'bookmarks';
protected $touches = ['user','record'];
protected $fillable = ['record_id','user_id'];
protected $dates = ['updated_at','created_at','deleted_at'];
public function user()
{
return $this->belongsTo(User::class);
}
public function record()
{
return $this->belongsTo(Record::class);
}
}
Everything is okey when the owner softDelete his record, The record un-indexed from Meilisearch Bookmarks Model and From User->bookmarks, the problem isnothing change for all others users who bookmarks this record, Meaning it is touching only her owner.
I tried to iterate all user->bookmarks to update the relationship but this solution it takes a lot of time and effort, and frankly, I do not consider it reasonable if the number of Bookmarkers is large.
Record.php
protected static function boot()
{
parent::boot();
static::softDeleted(function ($model) {
$model->images()->delete();
$model->bookmarks()->delete();
$model->user->update();
$model->images->unsearchable();
$model->bookmarks->unsearchable();
$bookmarkIds = $model->bookmarks()->pluck('id')->toArray();
Bookmark::whereIn('id', $bookmarkIds)->unsearchable();
)};
static::restored(function ($model) {
$model->images()->restore();
$model->bookmarks()->restore();
$model->user->update();
$model->images->searchable();
$model->bookmarks->searchable();
$model->searchable();
$bookmarkIds = $model->bookmarks()->pluck('id')->toArray();
Bookmark::whereIn('id', $bookmarkIds)->searchable();
});
}
I'm skeptical whether that's the problem is from my Relationship design or Laravel scout or Meilisearch !
When I check Laravel Eloquent works fine
Inside Meilisearch
please any suggest or solution.
Thanks
Hello @seifrached
this repository is not for support, and we have duplicated content here I'm closing it