laravel 如何检查一个记录是否附加到另一个记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37142235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if one record is attached to another?
提问by Alexander Lomia
I have defined a many-to-many relationship between Student
and Seminar
models. How can I find out if one particular studentis attending one particular seminar? Non-working example of what I want to achieve is illustrated below:
我已经定义了Student
和Seminar
模型之间的多对多关系。我如何才能知道某个特定的学生是否正在参加某个特定的研讨会?我想要实现的非工作示例如下所示:
$seminar->students()->contains($student->id);
The following error is shown when using the above code snippet:
使用上面的代码片段时显示以下错误:
BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::contain()'
BadMethodCallException 带有消息“调用未定义的方法 Illuminate\Database\Query\Builder::contain()”
回答by Matt McDonald
Try instead:
试试吧:
$seminar->students->contains($student->id);
Presuming $student
contains an instance of the Student
model you can simplify it further and just use:
假设$student
包含Student
模型的一个实例,您可以进一步简化它,只需使用:
$seminar->students->contains($student);
When you add parentheses you're using the Laravel query builder, and you never completed the query. To do it in the manner you had originally you would need:
当您添加括号时,您正在使用 Laravel 查询构建器,并且您从未完成查询。要按照您最初的方式进行操作,您需要:
$seminar->students()->get()->contains($student->id);
This method could be useful if you wanted to add constraints when fetching your students.
如果您想在获取学生时添加约束,此方法可能很有用。
But generally you can omit the parentheses and a Collection will be returned, allowing you to use methods like contains
.
但通常您可以省略括号,并返回一个集合,允许您使用像contains
.
This has the additional benefit of not re-querying the database once the relationship is loaded, so will generally be a far more efficient means of fetching relationships.
这有一个额外的好处,即一旦加载关系就不会重新查询数据库,因此通常是一种更有效的获取关系的方法。
If you haven't already loaded the students and want a database efficient method of checking you could instead use:
如果您还没有加载学生并想要一种数据库有效的检查方法,您可以使用:
$seminar->students()->where('students.id', $student->id)->exists();
回答by Tharaka Dilshan
In your case the exception is you are calling 'contains()' function (which is for Laravel Collection) on 'Query Builder'. It should be
在您的情况下,例外是您在“查询生成器”上调用“contains()”函数(用于 Laravel 集合)。它应该是
$seminar->students->get()->contains($student->id);
but this is inefficient since this will retrieve all the students of the seminar.
但这效率低下,因为这将检索研讨会的所有学生。
so instead,
所以与其,
$seminar->students()->wherePivot('student_id', $student->id)->exists();
this method will check in the intermediate table of many to many relationship for particular seminar-student pair, and will return whether exists or not.
此方法将检查特定研讨会学生对的多对多关系的中间表,并返回是否存在。
回答by rzb
The accepted answer is wrong. $seminar->students()->exists($student->id)
does not check if the relationship exists. It only checks if the student exists. The student could belong to any seminar and it would still return true.
接受的答案是错误的。$seminar->students()->exists($student->id)
不检查关系是否存在。它只检查学生是否存在。学生可以属于任何研讨会,它仍然会返回 true。
The correct way to check if a relationship exists without fetching records from the database would be:
在不从数据库中获取记录的情况下检查关系是否存在的正确方法是:
$seminar->students()->whereId($student->id)->exists()
$seminar->students()->whereId($student->id)->exists()