jorgehaidar's avatar

Model::with select relationship's column return null

I have a disposition model that is related to disposition_type. disposition has a disposition_type_id column.

dispositions columns-> id, name, service, ..., disposition_type_id

disposition_types columns -> id, name, active ...

disposition Class

class Disposition extends Model
{
	...
    public function dispositionType(): BelongsTo
    {
        return $this->belongsTo(DispositionType::class);
    }
	...
}

DispositionType Class

class DispositionType extends Model
{
	...
    public function dispositions(): HasMany
    {
        return $this->hasMany(Disposition::class);
    }
	...
}

when i run this code It works perfect

Disposition::query()->with(
        ["dispositionType"]
    )->first();

this is the answer

...
"actions_id" => 2
"disposition_status_id" => 2
"disposition_type_id" => 4
"active" => 1
"created_at" => "2024-07-27 03:19:28"
"updated_at" => "2024-07-27 03:19:28"
"disposition_type": {
      "id": 4
      "name": "plan"
      "active": 0
      "created_at": "2024-07-27T07:19:25.000000Z"
      "updated_at": "2024-07-27T07:19:25.000000Z"
   }

but when i want only 1 column of dispositionType for example name

Disposition::query()->with(
        ["dispositionType:name"]
    )->first();

this return null

"actions_id" => 2
"disposition_status_id" => 2
"disposition_type_id" => 4
"active" => 1
"created_at" => "2024-07-27 03:19:28"
"updated_at" => "2024-07-27 03:19:28"
"disposition_type": null

So anybody know what's the reason?

Thanks!

0 likes
5 replies
Tray2's avatar

Add ->select('name') to your relation definition.

 public function dispositionType(): BelongsTo
    {
        return $this->belongsTo(DispositionType::class)->select('name');
    }

jorgehaidar's avatar

@Tray2 thanks but, I cannot use select within the definition of the relationship, it could cause errors in other uses of the relationship

Snapey's avatar
Snapey
Best Answer
Level 122

you must include the key in the select


Disposition::query()->with(
        ["dispositionType:id,name"]
    )->first();
1 like
jorgehaidar's avatar

@Snapey Thank you very much; it worked perfectly. However, according to the Laravel documentation, to use the eager loading with specific columns feature, you always have to include the ID or the foreign key. But I have a many-to-many relationship between disposition and category and subcategories, and I was able to use this functionality in the following way:

Disposition::query()->with(
            [
                'categories:name',
                'subcategories:name'
            ]
        )
        ->findOrFail($id);

and this runs without errors and responds correctly

Snapey's avatar

@jorgehaidar because you are not running a select on the pivot table, only categories

So the query in this case will automatically use the disposition_id and the category_id from pivot and then

select name from categories where id = pivot.category_id

In your original query, eloquent gets the Disposition model, then gets the DispositionType (two collections) but the Disposition_type does not have the id to allow eloquent to merge the two models.

Please or to participate in this conversation.