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

lirone's avatar

Eloquent multiple relationship return attributes array

I have three tables : sensors, equipement, location

In sensor Table, I have

	public function equipement()
	{
		return $this->belongsTo(Equipement::class);
	}

In Equipement table, I have

	public function location()
	{
		return $this->belongsTo(Location::class);
	}

What I would like to do is returning in one query all the informations regarding the sensor + the equipement association and the location associated to this equipement.

I did something like


    public function all()
    {
        $sensors = Sensor::with('equipement', 'equipement.location')->get();

        return $sensors;
    }

It seems to works. However, when I want to loop in Blade, the equipement and location are not objects but array.

@foreach ($sensors as $sensor)
             <tr>
            <td>{{ $sensor->id }}</td> //Working
            <td>{{ $sensor->device_number }}</td>  //Working
             <td>{{ $sensor->installation_date }}</td>  //Working
             <td>{{ $sensor->equipement->name}}</td> //This is not working. I need instead to do  $sensor->equipement['name']
<td>{{ $sensor->equipement->location->name}}</td> //This is not working.

</tr>

In Tinker, when I do $sensors = Sensor::with('equipement', 'equipement.location')->first();, I'm able to access as an object the attribute of the equipement and the location. I know, I'm getting the object with first() but with get(), as I loop, it should be the object as well no ?

Could you please help me ?

In Blade, I would like to access attributes as an object and not array. Thank you.

0 likes
3 replies
ahmeddabak's avatar

Normally you will have an model object returned, you must have something in your model that changing the default behavior.

Please post the complete code of the Sensor and Equipement models.

ahmeddabak's avatar

It is a belongsTo relation, it should return a Model object and not a collection

lirone's avatar

Here the sensor class

<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

class Sensor extends Model
{
	protected $table = 'sensors';
	public $timestamps = false;

	protected $casts = [
		'equipement_id' => 'int',
	];

	protected $dates = [
		'installation_date'
	];

	protected $fillable = [
		'equipement_id',
		'deveui',
	];



	public function equipement()
	{
		return $this->belongsTo(Equipement::class);
	}



	public function pools()
	{
		return $this->hasMany(Pool::class);
	}
	public function records()
	{
		return $this->hasMany(Record::class);
	}

	public function sensor_groups()
	{
		return $this->hasMany(SensorGroup::class);
	}

	public function sensor_settings()
	{
		return $this->hasMany(SensorSetting::class);
	}
}

And Equipement Class

<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

class Equipement extends Model
{
	protected $table = 'equipement';
	public $timestamps = false;

	protected $casts = [
		'equipement_id' => 'int',
		'latitude' => 'float',
		'longitude' => 'float',
	];

	protected $fillable = [
		'location_id',
		'name',

	];



	public function location()
	{
		return $this->belongsTo(Location::class);
	}

	public function sensors()
	{
		return $this->hasMany(Sensor::class);
	}
}

Thank in advance !

Please or to participate in this conversation.