有没有办法软删除'多对多关系?我已经在我的数据透视表中添加了deleted_at
列,并使用了SoftDeletingTrait
。但是当您detach
这两个关系时,该行将被完全删除。
我有表clients
,users
和client_user
。我希望以某种方式,使用User::find($user_id)->detach($client_id)
实际上不会删除数据行,但将deleted_at设置为当前时间戳。
然后在此之后继续,我不希望能够恢复软删除的项目。但是,目前,即使我手动设置deleted_at
的值,我仍然得到结果
答案 0 :(得分:3)
DB::table('client_user')
->where('client_id', $client->id)
->where('user_id', $user->id)
->update(array('deleted_at' => DB::raw('NOW()')));
答案 1 :(得分:2)
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
class User extend Model
{
public function clients(): Relation
{
return $this
->belongsToMany(
Client::class,
'client_user',
'user_id',
'client_id'
)
->using(ClientUserPivot::class)
->wherePivot('deleted_at', null)
// load pivot ID (otherwise deleted event won't fire)
// load more pivot fileds if we need
->withPivot('id', 'is_enabled', 'extra')
->withTimestamps();
}
}
/* The Cilent model file, in a similar way */
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
class Client extend Model
{
public function users(): Relation
{
return $this
->belongsToMany(
User::class,
'client_user',
'client_id',
'user_id'
)
->using(ClientUserPivot::class)
->wherePivot('deleted_at', null)
// load pivot ID (otherwise deleted event won't fire)
// load more pivot fileds if we need
->withPivot('id', 'is_enabled', 'extra')
->withTimestamps();
}
}
/* The Pivot file */
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\SoftDeletes;
class ClientUserPivot extends Pivot
{
use SoftDeletes;
public $incrementing = true;
protected $table = 'client_user';
protected $dates = ['deleted_at'];
public function client(): Relation
{
return $this->belongsTo(Client::class);
}
public function user(): Relation
{
return $this->belongsTo(User::class);
}
}
/////////////////////////
// We can see that the Pivot class is extends Model class,
// it just use the AsPivot trait, set the $incrementing = false,
// and set the $guarded = [].
// So, we can run the methods of Model in Pivot entity, such as @delete.
// Working:
$user = User::find(1);
$client = $user->clients->firstWhere('id', $clientId);
if ($client) {
$client->pivot->delete();
}
// Last:
// You may have seen that I added a 'is_enabled' field in the pivot table,
// I think it may better than SoftDeletes.
答案 2 :(得分:0)
是的,您可以在数据透视表上制作软删除,您只需要为他们提供自己的ID和时间戳。那样" SoftDeletingTrait"能行得通。这有点棘手,但对我有用。 编辑:
而且我也忘了提到你必须为数据透视表制作一个模型,正如有人告诉你上面的= P例如,你有一个信用额度,以及许多用于该信用额度的许可证credit_lines,但是你想知道每一行上的requeriments的状态,还要从该信用额度中删除,而不是从其他行中删除。
Table
credit_line
id
name
capital
[More columns]
Table
requirements (Documents and things that everybody uses)
id
name
[More columns]
Table (pivot)
credit_line_has_requirements
id <---- you need this...
timestamps <---- also this
status_id <---- declined, accepted etc...
reason <---- a text description of why you declined or acepted the requeriment...
然后使用该数据透视表为CreditLineHasRequeriments创建一个模型。
抱歉英语不好。