laravel 在Eloquent中软删除多对多关系的透视

hvvq6cgz  于 2022-11-18  发布在  其他
关注(0)|答案(4)|浏览(135)

是否有方法可以“软删除”多对多关系?我已经向数据透视表中添加了一个deleted_at列,并且正在使用SoftDeletingTrait。但是,当您对这两个关系执行detach操作时,该行将被完全删除。
我有表clientsusersclient_user,我希望使用User::find($user_id)->detach($client_id)不会真正删除透视行,而是将deleted_at设置为当前时间戳。
然后继续,我不希望能够得到软删除的项目回来。但是,目前,即使我手动设置deleted_at的值,我仍然得到结果

h22fl7wq

h22fl7wq1#

DB::table('client_user')
                ->where('client_id', $client->id)
                ->where('user_id', $user->id)
                ->update(array('deleted_at' => DB::raw('NOW()')));
sd2nnvve

sd2nnvve2#

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.
q7solyqu

q7solyqu3#

是的,你可以在数据透视表上进行软删除,你所需要的只是给它们自己的id和时间戳。这样“SoftDeletingTrait”就可以工作了。这有点棘手,但对我来说很有效。编辑:
我还忘了提到,您必须为数据透视表创建一个模型,就像上面有人告诉您的那样=P。例如,您有一个信用额度,以及该信用额度的多个请求,这些请求用于多个credit_lines,但您希望了解每一行上请求的状态,以及从该信用额度中删除的请求,而不是从其他信用额度中删除的请求。

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创建一个模型。
抱歉我的英语不好。

s8vozzvw

s8vozzvw4#

如果您需要在数据透视表中使用softDeletes特征,则必须扩展数据透视模型vai Laravel的常规模型,而不是数据透视模型,即

YourPivotModal extends Illuminate\Database\Eloquent\Model; instead of 

YourPivotModal extends Illuminate\Database\Eloquent\Relations\Pivot;

此外,必须在透视表中定义deleted_at列

相关问题