Search code examples
phplaraveleloquentlumeneloquent-relationship

Get model which has all specific related models


I have no idea how to solve my problem.

I have Model1 with relation:

class Model1 extends BaseModel
{
    public function details(): BelongsToMany
    {
        return $this->belongsToMany(
            Detail::class,
            'model1_details',
            'model1_id',
            'detail_id',
        );
    }
}

model1_details is my pivot table.

Also I have array of details ids like [1, 2, 3]. I need to fetch Model1 that belongs to ALL Detail with given ids.

I need to do this inside my filter. That's what I have inside my controller:

$builder = Model1::filter(new ModelListFilter($request));

and inside ModelListFilter:

protected function filter(): Builder
    {
        $request = $this->request;

        $request->whenFilled('details', function ($query) {
            //
        });

        return $this->builder;
    }

I've tried:

$request->whenFilled('details', function ($query) {
            foreach($query as $detailId) {
                $this->builder->whereHas('details', function (Builder $innerQuery) use ($detailId) {
                    $innerQuery->where('detail_id',  $detailId);
                });
            }
        });

But it returns all models Model1 even without any details.

UPD

So the problem wasn't there =) 'details' just wasn't filled in my Request. also my $query was a string, not array, so I called json_decode on it.

Code above retrieves Models belonging to detail with id=1 AND to detail with id=2 and so on.

But I think there might be better solution so I'll leave this question open

UPD 2

also changed this

$innerQuery->where('detail_id',  $detailId);

to this

$innerQuery->where('id',  $detailId);

so here is needed to pass columns we have in 'details' table, not columns from pivot table


Solution

  • Found solution to my problem here. So it seems like there is no better solution(