有条件的地方基于另一个模型的价值

编程入门 行业动态 更新时间:2024-10-27 18:33:05
本文介绍了有条件的地方基于另一个模型的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有三种型号

发票

工作室

R_studio_pay

关系如下

工作室有很多发票

工作室有一个 R_studio_pay

studio hasOne R_studio_pay

如果R_studio_pay中的is_r列为1,我需要获取发票-> where('recurring','single')-> get(),否则我不希望where子句.

I need to get invoices->where('recurring', 'single')->get() if a column is_r is 1 in R_studio_pay else i don't want the where clause.

我尝试使用whereHas studio->在哪里R_studio_pay但有条件的地方无法完成.

I tried using whereHas studio-> wherehas R_studio_pay but conditional where cannot be done.

$invoices = invoice::with('studio')->whereHas('studio', function($query) { $query->whereHas('r_studio_pay', function($query) { $query->where('is_r', 1); }); })->where('recurring', 'single')

但是不能在有条件的地方应用.

But couldn't apply conditional where.

推荐答案

您处在正确的轨道上.但是,您应该在回调中添加return语句. return $query->where('is_r', 1);代替$query->where('is_r', 1);.尝试下面的代码.

You are on the right track. However, you should add a return statement in your callbacks. return $query->where('is_r', 1); instead of $query->where('is_r', 1);. Try the code below.

$invoices = invoice::with('studio')->whereHas('studio', function($query){ return $query->whereHas('r_studio_pay', function($query){ return $query->where('is_r', 1); }); })->where('recurring', 'single');

更多推荐

有条件的地方基于另一个模型的价值

本文发布于:2023-11-22 04:12:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615934.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有条件   模型   价值   地方

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!