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

nurularifin's avatar

Why Error Attempt to read property "title" on null

Hi everyone, please help me, i got the error Attempt to read property "title" on null. Is anyone know why I got this error?, the following is my code:

Controller

public function customerdetail($id)
    {
        $customer   = Customer::findOrFail($id);
        return view('admin.detail-customer',[
            'customer'  => $customer
        ]);
    }

At Blade

@foreach($customer->transactions as $transaction)
<tr>
    <td>{{ $transaction->course->title }}</td>
</tr>

This is my model:

Transaction Model:
public function schedule()
    {
        return $this->belongsTo(Ul_coursemeta::class);
    }

    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }

    public function course()
    {
        return $this->belongsTo(Course::class);
    }

Customer model:
 public function transactions()
    {
        return $this->hasMany(Transaction::class);
    }

Course model:
public function schedules()
    {
        return $this->hasMany(Ul_coursemeta::class);
    }

Please help me. Thank u so much.

0 likes
7 replies
Sinnbeck's avatar

Because you have a transaction without a course

<td>{{ $transaction->course->title ?? 'no course!' }}</td> 
Sinnbeck's avatar

Also you should eager load. Otherwise you will make alot of database queries

$customer   = Customer::with('transactions.course')->findOrFail($id);
nurularifin's avatar

@Sinnbeck thank u for your response, at least I know what's wrong, I'll try my best to solve this.

Sinnbeck's avatar

@nurularifin output the course id to see which one is missing

<td>{{$transaction->course_id}} - {{ $transaction->course->title ?? 'no course!' }}</td>  
Sinnbeck's avatar

@nurularifin are you sure transactions have a course id? It should be shown on the page as well if you tested my code

nurularifin's avatar

@Sinnbeck I have build the query and this is my controller look like:

public function customerdetail($id)
    {
        $transactions   = Transaction::select(
            'transactions.id AS TRANSID',
            'customers.full_name AS NAME',
            'ul_coursemeta.price AS PRICE'
        )
            ->join('customers','customers.transaction_id','=','transactions.transaction_id')
            ->join('ul_coursemeta','transactions.dcourse_id','=','ul_coursemeta.id')
            ->where('customer_id',$id)
            ->get();

        return view('admin.detail-customer',[
            'transactions'  => $transactions
        ]);
    }

The following is the results as I want:

TRANS_ID  CUSTOMER      HARGA                        
--------  ------------  --------
      18  		Nurul Arifin      10000.00

So the problem only on to display course data, how to display them, the following my table look like:

Course Table
==========
id | Title

Ul_coursemeta
===========
id | course_id

Sorry if my table name makes you confused.

Please or to participate in this conversation.