我正在尝试使用雄辩的关系从我的数据库中检索一些数据。尝试获取特定字段而不是完整对象时遇到问题。这是我的一些代码。
控制器:
    public function getHosts($table = null)
    {
            foreach (Hosts::all() as $host)
            {
               echo $host->physical_machine_name;
               echo $host->app->id;
            }
     }
楷模:
class Hosts extends Eloquent
{
    protected $connection = 'mysql_2';
    protected $table = 'hosts';
    public $timestamps = false;
    public function app(){
            return $this->hasMany('Apps', 'os_instance_name')->orWhere('os_instance_name', $this->os_instance_name);
    }
}
class Apps extends Eloquent
{
    protected $connection = 'mysql_2';
    protected $table = 'app_instances';
    public $timestamps = false;
     public function host()
     {
           return $this->belongsTo('Hosts','os_instance_name')->orWhere('os_instance_name', $this->os_instance_name);
     }
}
我无法获得要显示的应用程序的 ID,但是当我删除“-> id”说明符时,我能够获得一个包含所有字段的 json 对象。为什么会这样?
我还应该注意,$host->physical_machine_name工作正常。
这是我收到的错误:
Undefined property: Illuminate\Database\Eloquent\Collection::$id
我也无法使用急切加载,因为我在我的关系中使用自定义外键。