在 laravel 中调用未定义的方法 App\Student::contains()

Call to undefined method App\Student::contains() in laravel

在我的 blade 视图中,它给我一个错误 Call to undefined method App\Student:: contains()

我的控制器

if($auth->user()->getRoleNames() == '["Parent"]'){
    $id = $auth->user()->Parents_id;
    $parent = ParentName::find($id);
    $student = $parent->students;
    $announce = AnnounceMent::get();
    $roletype='parent';
    return view('announcement.index',compact('student','roletype','announce')); 
 }

我的查看文件

@foreach($announce as $ann)
    @if($student->contains('id', $ann->student_id))
        <tr>
            <td>{{$i}}</td>
            <td>{{$ann->announcement_type}}</td>
            <td>
                <?php $course = \App\Course::find($ann->course_id) ?>
                {{$course->course_name}}
            </td>
            <td>
            <?php $student = \App\Student::find($ann->student_id) ?>
                    {{$student['firstname'].' '.$student['lastname']}}
            </td>
            <td>{{$ann->description}}</td>
            <td>
            <a class="btn btn-success" href='{{ url("viewannounce/{$ann->id}") }}'>View</a>
            </td>
        </tr>
    @endif
@php($i++)
@endforeach

这是我的 collection 我得到 dd($students)

Collection {#595 ▼

#items: array:3 [▼

0 => Student {#596 }
1 => Student {#597 }
2 => Student {#598 }

] }

contains 是一个集合方法,你用非集合调用它,如果它是 App\Student::all()->contains('something') 就可以正常工作,但是 App\Student::find(1)->contains('something')App\Student::contains('something') 不会工作

Tinker 结果

>>> App\User::find(1)->contains('email')
BadMethodCallException with message 'Call to undefined method App/User::contains()'
>>> App\User::all()->contains('email')
=> false
>>> App\User::contains('email')
BadMethodCallException with message 'Call to undefined method App/User::contains()'
>>>

你可以

$student->id == $ann->student_id

我猜你的情况。