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

DInu98's avatar

Call to a member function get() on null

i need to select * from payment where st_id = 1 and I want display message if database records are empty "cannot found data " like this. but there is a message showing like this

Call to a member function get() on null

My view

<tbody>
    @if(!empty($payments))
        @foreach ($payments as $payment )
           <tr>
               <td>{{ $payment->student->FullName }}</td>
               <td>{{ $payment->student_id}}</td>
               <td>{{ $payment->course->Name}}</td>
               <td><img src="{{ asset('images/'.$payment->image_path) }}" alt="recipt image"></td>
               <td>{{ $payment->created_at->toDatestring(); }}</td>
               <td>
                   <a href="#" class="btn btn-success btn-circle btn-sm">
                       <i class="fas fa-check"></i>
                   </a>
                   Verified..
                </td>
            </tr>
         @endforeach
    @else
        <td>Cannot find data</td>
    @endif
</tbody>
     

Controller

 public function render()
    {
        $sid = Auth::user()->id;
        $payments = Payment::find($sid)->get();

        $courses = Auth::guard('student')->user()->courses()->get();
        $students = Auth::guard('student')->user()->get();
        
        return view('livewire.stud-payment',[
            'courses'=>$courses,
            'students'=>$students,
            'payments'=>$payments
            
        ]);
    }
0 likes
2 replies
frankielee's avatar
Level 29

$payments = Payment::find($sid)->get()

You will trigger another error here. Find only return single model.

Maybe you want to use the where to filter?

$courses = Auth::guard('student')->user()->courses()->get();

Not sure how you define the new guard. But make sure the user and coursers are not empty. Try to use ddd() or logger to debug

ddd(Auth::guard('student')->user(),Auth::guard('student')->user()->courses()->get());
1 like

Please or to participate in this conversation.