我需要重写 InteractsWithPivotTable Trait 中 BelongsToMany 中的同步函数
因为我想在分离时添加范围
<?php
use Illuminate\Database\Eloquent\Model;
class TableName extends Model
{
// number 1
/**
* Sync the intermediate tables with a list of IDs or collection of models.
*
* @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $ids
* @param bool $detaching
* @return array
*/
public function sync($ids, $detaching = true, callable $wherePivot = null)
{
}
// number 2
public function syncWhere($ids, callable $wherePivot = null)
{
// Custom logic here
// You can modify or add additional functionality
// Call the parent sync method to perform the default sync behavior
return parent::sync($ids, false);
}
}
但是这些不起作用,因为同步函数不在模型类中
答案是我必须扩展 BelongsToMany 关系类,然后在其中重写,然后使用它而不是原始 BelognsToMany 类