Laravel Eloquent 如何在输出中对 “with” 数据应用条件?

huangapple go评论76阅读模式
英文:

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

问题

以下是您的代码的翻译部分:

$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(),
];

问题在于输出中,当“service”和“facility”名称与$searchService和$searchPartenaire不相等时,这些值会被替换为"null"。因此,我不希望在输出中有与搜索变量不相等的值。感谢您。

英文:

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.

答案1

得分: 1

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

then apply the condition; this is a good approach in search.

英文:

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

答案2

得分: 0

这是Kamlesh Paul建议后的我的代码:

$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;

但是存在无限的HTTP请求调用问题,我认为问题可能出在当where条件找不到相等的名称时。有人有建议吗?谢谢。

英文:

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.

答案3

得分: 0

我终于找到了一个非常相似的解决方案:

$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(),
];
感谢您的帮助。
英文:

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.

huangapple
  • 本文由 发表于 2020年1月3日 17:45:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定