【发布时间】:2014-08-18 15:24:43
【问题描述】:
在处理 Laravel 的关系时,我经常使用这样的东西:
User::where(...)
->first()
->securityQuestions()
->take(2)
->orderBy(DB::raw('RAND()'))
->get();
注意使用securityQuestions() 而不是securityQuestions,否则会引发错误:
Missing argument 1 for Illuminate\Support\Collection::get()
现在,在其他部分我可以使用:
User::where(...)
->first()
->securityQuestions
->contains(...)
注意使用securityQuestions 而不是securityQuestions()。
我的猜测是,为了从数据库中检索内容(并继续链接wheres 和内容),您必须在属性具有不可变结果集时使用该方法。有谁知道这是否正确?
【问题讨论】:
-
什么是安全问题?自定义函数?还是表格列?
-
@TheShiftExchange - 如果我猜的话,用户与 SecurityQuestion 是一对多的关系。
-
您能否尝试使用 DB::getQueryLog(); 打印在后台为这两个查询生成的 SQL?查看此链接stackoverflow.com/a/19085511/3007014。然后你可以比较有什么不同。
-
@TheShiftExchange 引用
security_questions表的模型,与users表的多对多关系。