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

toxican's avatar

Struggles with "belongsTo()"

So I've got a Client model and a MaintenanceEntry model. I've got a pivot table setup to accommodate the relationship (a MaintenanceEntry belongsTo a single Client, but a Client hasMany MaintenanceEntry(s)). What I'm trying to do is fetch the Client id associated with an individual MaintenanceEntry, but when I try to call it, I just get null. This is how I'm calling it:

$maintenance_entry->client->id

but I've also tried

$maintenance_entry->client

//OR

$maintenance_entry->client()

//OR

$maintenance_entry->client()->id

and no luck! I feel like I'm having a hard time wrapping my mind around relationships. Fortunately whenever I've been able to trigger an error, the SQL errors were really useful in learning how to properly structure my databases to accommodate relationships. But like I said, it's just returning nul..

MaintenanceEntry Model:

<?php
namespace Cole;
use Illuminate\Database\Eloquent\Model;

class MaintenanceEntry extends Model{

    protected $table = "maintenance_entries";
    protected $fillable = ['type_id', 'notes'];

    public function type(){
        return $this->belongsTo('Cole\MaintenanceType');
    }

    public function client(){
        return $this->belongsTo('Cole\Client', 'client_id');
    }

}

Client Model:

<?php
namespace Cole;
use Illuminate\Database\Eloquent\Model;

class Client extends Model{

    protected $fillable = ['client','name','hosting','address','phone_1','phone_2','email_address'];

    public function contacts(){
        return $this->belongsToMany('Cole\Contact');
    }

    public function hosting(){
        return $this->belongsTo('Cole\Hosting');
    }

    public function maintenance(){
        return $this->belongsToMany('Cole\MaintenanceEntry');
    }

}

Then my DB is structured like this:

clients table: id (unsigned, primary, ai) name (varchar) website (varchar) hosting_id (int)

client_maintenance_entry table:

id (unsigned, primary, ai)

client_id (unsigned, FK->clients.id)

maintenance_entry_id(unsigned, FK->maintenance_entries.id)

maintenance_entries table:

id (usigned, primary, ai)

type_id

notes

0 likes
2 replies
ctroms's avatar
ctroms
Best Answer
Level 15

Pivot tables are used for many to many relationships. (e.g. a client has many maintenance entries and a maintenance entry can belong to many clients). Since you are only establishing a one to many relationship, (a client has many maintenance entries and a maintenance entry belongs to one client), you don't need the pivot table.

First, on the maintenance_entries table, make sure to define a foreign key reference to your clients table.

Then modify your model relationship methods a bit.

Client

public function maintenanceEntries() {
    return $this->hasMany('App\MaintenanceEntry');
}

MaintenanceEntry

public function client() {
    return $this->belongsTo('App\Client');
}

I would also encourage you to revisit the docs again. There is nothing wrong with reading something multiple times to try to solidify your understanding.

Also check out the Laracast video Eloquent Relationships. You might even watch the whole series. As always, there is a lot of good stuff in Jeffrey's videos.

toxican's avatar

Thank you! I was able to re-structure things around and get things working in just a few minutes.

I definitely need to give the docs (yet another) look :) Also the video, as its been a while since I've given it a watch.

1 like

Please or to participate in this conversation.