首页 \ 问答 \ 在雄辩的laravel中基于来自子模型的参数查询第一模型(Query first model based on parameters from child model in eloquent laravel)

在雄辩的laravel中基于来自子模型的参数查询第一模型(Query first model based on parameters from child model in eloquent laravel)

我有2个用户表与相应的UserBUser雄辩模型。 我正在尝试查询User并仅根据BUser上的参数返回结果。

我目前正在这样做:

public function scopeMarketplace($query) {
    return $query->with(['buser' => function($q) {
        $q->where('marketplace', '=', 1);
    }]);
}

如果marketplace = 1那么返回所有User但仅包括BUser但我不想返回任何User除非满足相应的BUser参数。 SQL最终成为:

select * from `users` limit 50 offset
select * from `busers` where `busers`.`id` in ('62', '63', '99', '100', '101', '102', '104', '105', '106', '107', '108', '109', '110', '111', '113', '114', '115', '116', '117', '118', '126', '128', '130', '131', '132', '142', '144', '145', '146', '147', '148', '149', '150', '151', '152', '153', '154', '155', '156', '157', '158', '159', '165', '166', '167', '168', '169', '170', '171', '172') and `marketplace` = '1'

这将获取所有User对象,然后查询BUser - 因此返回错误的数据。

UserBUser的关系是:

/**
 * @return \Illuminate\Database\Eloquent\Relations\HasOne
 */
public function buser()
{
    return $this->hasOne('App\BUser', 'id', 'id');
}

I have 2 users tables with corresponding User and BUser eloquent models. I'm trying to query User and only return results based on parameters on BUser.

I'm currently doing this:

public function scopeMarketplace($query) {
    return $query->with(['buser' => function($q) {
        $q->where('marketplace', '=', 1);
    }]);
}

Which is returning all Users but only including BUser if marketplace = 1 but I don't want to return any Users unless the corresponding BUser parameters are met. The SQL ends up being:

select * from `users` limit 50 offset
select * from `busers` where `busers`.`id` in ('62', '63', '99', '100', '101', '102', '104', '105', '106', '107', '108', '109', '110', '111', '113', '114', '115', '116', '117', '118', '126', '128', '130', '131', '132', '142', '144', '145', '146', '147', '148', '149', '150', '151', '152', '153', '154', '155', '156', '157', '158', '159', '165', '166', '167', '168', '169', '170', '171', '172') and `marketplace` = '1'

This gets all User objects and then query's BUser - so returns the wrong data.

The relationship in User for BUser is :

/**
 * @return \Illuminate\Database\Eloquent\Relations\HasOne
 */
public function buser()
{
    return $this->hasOne('App\BUser', 'id', 'id');
}

原文:https://stackoverflow.com/questions/32331730
更新时间:2023-05-25 08:05

最满意答案

(- 1)是一个表达式 ,其值为 -1 。 引用(在你的代码前面)阻止了表达式评估 。 因此:

> '((- 1) 2 3)
'((- 1) 2 3)

> (list (- 1) 2 3)
'(-1 2 3)

或者,如果您阅读了quasiquoting并想炫耀

> `(,(- 1) 2 3)
'(-1 2 3) 

(- 1) is an expression that evaluates to -1. Quoting (the ' in front of your code) prevents the evaluation of an expression. Therefore:

> '((- 1) 2 3)
'((- 1) 2 3)

but

> (list (- 1) 2 3)
'(-1 2 3)

or, if you read up about quasiquoting and you want to show off

> `(,(- 1) 2 3)
'(-1 2 3) 

相关问答

更多
  • (- 1)是一个表达式 ,其值为 -1 。 引用(在你的代码前面)阻止了表达式的评估 。 因此: > '((- 1) 2 3) '((- 1) 2 3) 但 > (list (- 1) 2 3) '(-1 2 3) 或者,如果您阅读了quasiquoting并想炫耀 > `(,(- 1) 2 3) '(-1 2 3) (- 1) is an expression that evaluates to -1. Quoting (the ' in front of your code) prevents ...
  • 而不是编写一个函数来做这个检查,你应该可以使用这个表达式: (number < 0) Javascript会评估此表达式,首先尝试将左侧转换为数值,然后再检查它是否小于零,这似乎是您想要的。 规格和细节 x < y的行为在§11.8.1“小于运算符( < )”中指定 ,使用§11.8.5抽象关系比较算法 。 如果x和y都是字符串,情况会有很大的不同,但由于右侧已经是一个数字(number < 0) ,所以比较将尝试将左边的一个数字转换成一个数字进行比较。 如果左侧无法转换为数字,则结果为false 。 请 ...
  • 你可以使用列表推导: >>> some_list = [-5, -1, -13, -11, 4, 8, 16, 32] >>> max([n for n in some_list if n<0]) -1 >>> min([n for n in some_list if n>0]) 4 You can just use list comprehensions: >>> some_list = [-5, -1, -13, -11, 4, 8, 16, 32] >>> max([n for n in some ...
  • 您的“否”列将以-1的AutoIncrementSeed和-1的AutoIncrementStep开头。 DataSet不够聪明,以“No”列的最大值开始,因此您需要以编程方式设置它。 Me.MyDataSet.MyDataTable.Columns("No").AutoIncrementSeed = _ Me.MyDataSet.MyDataTable.Max(Function(Row) Row.No) + 1 Me.MyDataSet.MyDataTable.Columns("No").AutoInc ...
  • 很大程度上取决于你想象客户试图通过什么类型的论证。 如果它们传递的是整数,并且这个值足够大以保存要使用的值的范围,那么使用std :: size_t没有实际的好处 - 它不会执行任何操作,并且问题的表现方式因为一个显然很庞大的数字更容易混淆。 但是 - 无论如何,最好使用size_t,因为它有助于记录API的期望。 你显然不能对运行时生成的值进行“> 0”的编译时检查,但至少可以消除来自有意识的巨大数字ala的负面输入 template void f(T t) { if ( ...
  • 对于imageview使用android:layout_gravity="right" ,对textview使用android:layout_gravity="left" 。
  • 你是这个意思吗? x = [1,-7, 9, 3, 6, -3 ] y = [elem for elem in x if elem < 0] z = [elem for elem in x if elem > 0] print(y) # -> [-7, -3] print(z) # -> [1, 9, 3, 6] 在这种特殊情况下,您可以使用稍微不同的方法并同时创建两个列表: x = [1,-7, 9, 3, 6, -3 ] yz = y, z = [], [] for elem in x: ...
  • 因为一个函数只能返回一次。 return语句终止函数的执行并将控制返回给调用函数。 在紧接呼叫之后的位置,呼叫功能继续执行。 所以在第8行,你将返回总和然后存在。 它不会进行循环的下一次迭代,因此它返回的第一个数字对于该条件是正确的。 解: 将每个迭代结果存储在列表,字典等其他位置,然后返回最后的函数。 在这里你可以尝试: #creat list using function def Negative(List): neg_num=[] for i in List: if i ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。