我想知道是否有函数或其他东西,您可以在其中从集合中获取主键以外的其他元素......例如,如果投票具有外键"user_id",我该如何检查?在 laravel 文档中,只有一个使用 contains() 检查主键的示例。谁能帮我?
检查是否存在 id = 2 的投票的示例
@foreach($projects as $project)
@if ($project->votes->contains(2))
//
@endif
@endforeach
我想检查是否有投票具有"user_id"=登录的用户ID
@foreach($projects as $project)
@if ($project->votes->contains('user_id' == Auth::id()))
//
@endif
@endforeach
if ($votes->contains('user_id', auth()->id())) {
//
}
在您的模型中
public static checkForeign($thisId) {
( $thisId == Auth::user()->id ) ? return true : return false;
}
在视图中
@if ( ModelName::checkForeign($project->votes->id) )
// Do something
@endif