通过pivot查询特定记录没有关系的地方?

Query where there is no relation for specific record through pivot?

几个小时以来一直试图解决这个问题,但似乎无法解决。

这是我要更改查询的显示方法

    public function show(DitoNumber $ditoNumber)
{
    $germanDismantlers = GermanDismantler::paginate(100);

    $relatedDismantlers = $ditoNumber->germanDismantlers;

    return view('admin.dito-numbers.show', compact('ditoNumber', 'germanDismantlers', 'relatedDismantlers'));
}

问题是我不想搞德国拆机我也在搞定关系。因此,与其仅仅获取所有记录并在方法的第一行对它们进行分页,我更愿意排除那些已经连接到 dito 号码的记录。

这里有点复杂的是它是通过一个枢轴连接的 table

这是table

    public function up()
{
    Schema::create('dito_number_german_dismantler', function (Blueprint $table) {
        $table->foreignId('dito_number_id')->constrained();
        $table->foreignId('german_dismantler_id')->constrained();

        $table->primary(['dito_number_id', 'german_dismantler_id'], 'pk');
    });
}

这是我将 germanDismantlers 连接到 ditoNumbers 的关系

public function germanDismantlers()
{
    return $this->belongsToMany(GermanDismantler::class);
}

反之

public function ditoNumbers()
{
    return $this->hasMany(DitoNumber::class);
}

更新 在这里,我正在尝试进行搜索。如果没有搜索或者如果它是明文的,这个查询工作正常,但问题是如果它是通过商业名称或数据(它去哪里的 orWhere)它忽略我们刚刚创建的 whereDoesntHave 并返回相关的拆解器再次

    if($search) {
         $germanDismantlers = GermanDismantler::whereDoesntHave(
                'ditoNumbers', function($query) use($ditoNumber, $search){
                    $query->where('id', $ditoNumber->id);
                    $query->where(function($innerQuery) use($search) {
                        $innerQuery->where('manufacturer_plaintext', 'like', '%' . $search . '%');
                        $innerQuery->orWhere('commercial_name', 'like', '%' . $search . '%');
                        $innerQuery->orWhere('date_of_allotment_of_type_code_number', 'like', '%' . $search . '%');
                    });
                }
            )
                ->paginate(100)
                ->withQueryString();
    }

如果我理解正确 - 您想要获取所有与 DitoNumber 无关的 GermanDismantler(s),然后对它们进行分页,对吗?

您可以尝试以下查询以获得所需的输出

public function show(DitoNumber $ditoNumber)
{
    //Get only those GermanDismantler records which are not associated with
    //the $ditoNumber record which is resolved from request parameter
    //Assume: id is PK column on table for DitoNumber
    $germanDismantlers = GermanDismantler::whereDoesntHave(
        'ditoNumbers', 
        fn($query) => $query->where('id', $ditoNumber->id)
    )
    ->paginate(100);

    $relatedDismantlers = $ditoNumber->germanDismantlers;

    return view('admin.dito-numbers.show', compact('ditoNumber', 'germanDismantlers', 'relatedDismantlers'));
}
更新:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'dito_numbers.german_dismantler_id' in 'where clause' Is it because dito_numbers does not have any fk? the fk's are both in the pivot table

这是因为您将 GermanDismantler 上的关系定义为 hasMany 而不是 belongsToMany

对于 Many-to-Many 关系,两个模型 类 应该具有 belongsToMany 关系。

将关系更改为

//GermanDismantler.php
public function ditoNumbers()
{
    return $this->belongsToMany(DitoNumber::class);
}

Laravel Docs - Querying Relationship Absence

Laravel Docs - Eloquent Relationships - Many-to-Many