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

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

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

问题

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

  1. $prestations = Prestation::with(
  2. [
  3. 'service' => function($service) use($searchService) {
  4. $service->select(['id','name'])->where('name', 'regexp', "/$searchService/i");
  5. },
  6. 'facility' => function($facility) use($searchPartenaire) {
  7. $facility->select(['id','name'])->where('name', 'regexp', "/$searchPartenaire/i");
  8. }
  9. ]
  10. )
  11. ->where('name', 'regexp', "/$search/i")
  12. ->orderBy($orderBy, $orderDirection)
  13. ->simplePaginate(50);
  14. $res = [
  15. 'results' => $prestations,
  16. 'total' => Prestation::all()->count(),
  17. ];

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

英文:

here my code :

  1. $prestations = Prestation::with(
  2. [
  3. 'service' => function($service) use($searchService) {
  4. $service->select(['id','name'])->where('name', 'regexp', "/$searchService/i");
  5. },
  6. 'facility' => function($facility) use($searchPartenaire) {
  7. $facility->select(['id','name'])->where('name', 'regexp', "/$searchPartenaire/i");
  8. }
  9. ]
  10. )
  11. ->where('name', 'regexp', "/$search/i")
  12. ->orderBy($orderBy, $orderDirection)
  13. ->simplePaginate(50);
  14. $res = [
  15. 'results' => $prestations,
  16. 'total' => Prestation::all()->count(),
  17. ];

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

  1. $prestations = Prestation::with('service','facility');
  2. $prestations->whereHas('service', function ($query) use ($searchPartenaire) {
  3. $query->Where('name', 'like', '%' . $searchPartenaire . '%');
  4. });
  5. $prestations->whereHas('facility', function ($query) use ($searchPartenaire) {
  6. $query->Where('name', 'like', '%' . $searchPartenaire . '%');
  7. });
  8. $prestations->where('name', 'like', '%'.$search.'%')
  9. ->orderBy($orderBy, $orderDirection)
  10. ->simplePaginate(50);
  11. return $res = [
  12. 'results' => $prestations,
  13. 'total' => Prestation::all()->count(),
  14. ];

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

then apply the condtion this is good approach in seach

答案2

得分: 0

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

  1. $prestations = Prestation::with('service','facility');
  2. $prestations->whereHas('service', function ($query) use ($searchService) {
  3. $query->where('name', 'regexp', "/$searchService/i");
  4. });
  5. $prestations->whereHas('facility', function ($query) use ($searchPartenaire) {
  6. $query->where('name', 'regexp', "/$searchPartenaire/i");
  7. });
  8. $prestations->where('name', 'regexp', "/$search/i")
  9. ->orderBy($orderBy, $orderDirection)
  10. ->simplePaginate(50);
  11. $res = [
  12. 'results' => $prestations,
  13. 'total' => Prestation::all()->count(),
  14. ];
  15. return $res;

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

英文:

Here my code after Kamlesh Paul suggestion :

  1. $prestations = Prestation::with('service','facility');
  2. $prestations->whereHas('service', function ($query) use ($searchService) {
  3. $query->where('name', 'regexp', "/$searchService/i");
  4. });
  5. $prestations->whereHas('facility', function ($query) use ($searchPartenaire) {
  6. $query->where('name', 'regexp', "/$searchPartenaire/i");
  7. });
  8. $prestations->where('name', 'regexp', "/$search/i")
  9. ->orderBy($orderBy, $orderDirection)
  10. ->simplePaginate(50);
  11. $res = [
  12. 'results' => $prestations,
  13. 'total' => Prestation::all()->count(),
  14. ];
  15. 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

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

  1. $prestations = Prestation::with('service', 'facility')
  2. ->whereHas('service', function ($query) use ($searchService) {
  3. $query->where('name', 'regexp', "/$searchService/i");
  4. })
  5. ->whereHas('facility', function ($query) use ($searchPartenaire) {
  6. $query->where('name', 'regexp', "/$searchPartenaire/i");
  7. })
  8. ->where('name', 'regexp', "/$search/i")
  9. ->orderBy($orderBy, $orderDirection)
  10. ->simplePaginate(50);
  11. $res = [
  12. 'results' => $prestations,
  13. 'total' => Prestation::all()->count(),
  14. ];
  15. 感谢您的帮助。
英文:

I finnaly found a solution very similar :

  1. $prestations = Prestation::with('service','facility')
  2. ->whereHas('service', function ($query) use ($searchService) {
  3. $query->where('name', 'regexp', "/$searchService/i");
  4. })
  5. ->whereHas('facility', function ($query) use ($searchPartenaire) {
  6. $query->where('name', 'regexp', "/$searchPartenaire/i");
  7. })
  8. ->where('name', 'regexp', "/$search/i")
  9. ->orderBy($orderBy, $orderDirection)
  10. ->simplePaginate(50);
  11. $res = [
  12. 'results' => $prestations,
  13. 'total' => Prestation::all()->count(),
  14. ];

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:

确定