duanhuang1699 2015-09-05 14:30
浏览 78
已采纳

Laravel 5.1:试图通过关系获取foreach循环中非对象的属性

I am trying to loop through a related model to my Users table call AdminNotes using a foreach in my blade view. However, I am getting the following error. Any ideas?

Trying to get property of non-object (View: /home/vagrant/Code/sass_l5/resources/views/users/edit.blade.php)

at HandleExceptions->handleError('8', 'Trying to get property of non-object', '/home/vagrant/Code/sass_l5/storage/framework/views/5da731c9bf1a02452259c329d3c273e0', '226', array('__path' => '/home/vagrant/Code/sass_l5/storage/framework/views/5da731c9bf1a02452259c329d3c273e0', '__data' => array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'user' => object(User)), 'obLevel' => '1', '__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'user' => object(User), 'note' => true)) in 5da731c9bf1a02452259c329d3c273e0 line 226

I have the relationships set up like this:

User Model:

...
class User extends Model
{
    ...
    public function adminNotes()
    {
        return $this->hasOne('sass\AdminNote');
    }
    ...
}

AdminNote Model:

...
class AdminNote extends Model {
  ...
  public function user()
  {
    return $this->belongsTo('sass\User');
  }
  ...
}

edit.blade.php

...
@if (count($user->adminNotes) >= 1)
  @foreach ($user->adminNotes as $note)
    {{ $note->created_at }}
    {{ $note->notes }}
  @endforeach
@else
...

dd($user->adminNotes):

AdminNote {#274 ▼


#connection: null
  #table: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:5 [▼
    "id" => 25
    "user_id" => 1
    "notes" => "Test note"
    "created_by" => 1
    "created_at" => "2014-01-01 00:00:00"
  ]
  #original: array:5 [▼
    "id" => 25
    "user_id" => 1
    "notes" => "Test note"
    "created_by" => 1
    "created_at" => "2014-01-01 00:00:00"
  ]
  #relations: []
  #hidden: []
  #visible: []
  #appends: []
  #fillable: []
  #guarded: array:1 [▶]
  #dates: []
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: true
  +wasRecentlyCreated: false
}

dd($note)

true
  • 写回答

1条回答 默认 最新

  • dounai9592 2015-09-05 14:47
    关注

    Thanks to Ross Wilson for pointing out that my relationships were set up to only have one note, where in they needed to be set up to have many notes.

    ...
    class User extends Model
    {
      ...
      public function adminNotes()
      {
        return $this->hasMany('sass\AdminNote');
      }
      ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加