Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shariff's avatar
Level 50

array_column() expects parameter 1 to be array, object given

I am getting this error in my view file

my Controller

if($auth->user()->getRoleNames() == '["Parent"]') {

        $id = $auth->user()->Parents_id;
        $parent = ParentName::find($id);

        $stu = $parent->students;

        $student = $stu->toArray();

        $announce = AnnounceMent::get();

             /*  echo "<pre>";
        print_r($student);
        echo "</pre>";    
         */

        $roletype='parent';
        return view('announcement.index',compact('student','roletype','announce')); 
    }

My view file

@foreach($announce as $ann)

            @if(array_search($ann->student_id,array_column($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

             

        @endforeach
0 likes
3 replies
bobbybouwmann's avatar
Level 88

Well $student here seems to be a collection of students. Because $stu = $parent->students; indicates that this returns multiple students. When you call toArray on a collection you get an array with arrays in there. In general, this shouldn't give the current error, but it also won't wind the result because that array with students doesn't have an id key.

Anyway, since you have collection methods you can do this way easier ;)

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

// View
@foreach($announce as $ann)

    @if ($students->contains('id', $ann->student_id))

        // Your HTML here

    @endif

@endforeach

Let me know if that works for you ;)

Please or to participate in this conversation.