Tony S
Tony S

Reputation: 571

Laravel Eloquent How to apply condition on "with" datas on the output?

here my code :

$prestations = Prestation::with(
            [
            'service' => function($service) use($searchService) {
                $service->select(['id','name'])->where('name', 'regexp', "/$searchService/i");
            },
            'facility' => function($facility) use($searchPartenaire) {
                $facility->select(['id','name'])->where('name', 'regexp', "/$searchPartenaire/i");
            }
            ]
        )
            ->where('name', 'regexp', "/$search/i")
            ->orderBy($orderBy, $orderDirection)
            ->simplePaginate(50);

        $res = [
            'results' => $prestations,
            'total' => Prestation::all()->count(),
        ];

The problem is that in the output of all datas where "service" and "facility" names are not equal on the $searchService and $searchPartenaire the values are replaced by "null". So i don't want to have values in the output where the search variables are not equals.

Thank you.

Upvotes: 0

Views: 120

Answers (3)

Tony S
Tony S

Reputation: 571

I finnaly found a solution very similar :

$prestations = Prestation::with('service','facility')
            ->whereHas('service', function ($query) use ($searchService) {
            $query->where('name', 'regexp', "/$searchService/i");
            })  
            ->whereHas('facility', function ($query) use ($searchPartenaire) {
                $query->where('name', 'regexp', "/$searchPartenaire/i");
            })
                ->where('name', 'regexp', "/$search/i")
                ->orderBy($orderBy, $orderDirection)
                ->simplePaginate(50); 

        $res = [
            'results' => $prestations,
            'total' => Prestation::all()->count(),
        ];

Thank's for your help.

Upvotes: 0

Tony S
Tony S

Reputation: 571

Here my code after Kamlesh Paul suggestion :

$prestations = Prestation::with('service','facility');

        $prestations->whereHas('service', function ($query) use ($searchService) {
            $query->where('name', 'regexp', "/$searchService/i");
        });

        $prestations->whereHas('facility', function ($query) use ($searchPartenaire) {
            $query->where('name', 'regexp', "/$searchPartenaire/i");
        });

        $prestations->where('name', 'regexp', "/$search/i")
            ->orderBy($orderBy, $orderDirection)
            ->simplePaginate(50);

        $res = [
            'results' => $prestations,
            'total' => Prestation::all()->count(),
        ];

        return $res;

But there is an infinite calls of http request, i think that the problem is when the where don't find an equal name, anyone have a suggest ? Thank's.

Upvotes: 0

Kamlesh Paul
Kamlesh Paul

Reputation: 12391

you can try like this

 $prestations = Prestation::with('service','facility');

        $prestations->whereHas('service', function ($query) use ($searchPartenaire) {
            $query->Where('name', 'like', '%' . $searchPartenaire . '%');
        });

        $prestations->whereHas('facility', function ($query) use ($searchPartenaire) {
            $query->Where('name', 'like', '%' . $searchPartenaire . '%');
        });

        $prestations->where('name', 'like', '%'.$search.'%')
            ->orderBy($orderBy, $orderDirection)
            ->simplePaginate(50);

        return $res = [
            'results' => $prestations,
            'total' => Prestation::all()->count(),
        ];

1st create instances of Prestation $prestations = Prestation::with('service','facility')

then apply the condtion this is good approach in seach

Upvotes: 1

Related Questions