Laravel 5.1: попытка получить свойство не-объекта в цикле foreach с отношением

Я пытаюсь пройти через связанную модель с моей таблицей пользователей, вызывающей AdminNotes, используя foreach в моем представлении блейда. Однако я получаю следующую ошибку. Любые идеи?

Попытка получить свойство не-объекта (Просмотр: /home/vagrant/Code/sass_l5/resources/views/users/edit.blade.php)

at HandleExceptions->handleError('8', 'Попытка получить свойство не-объекта', '/home/vagrant/Code/sass_l5/storage/framework/views/5da731c9bf1a02452259c329d3c273e0', '226', array('__path' = > '/home/vagrant/Code/sass_l5/storage/framework/views/5da731c9bf1a02452259c329d3c273e0', '__data' => массив ('__env' => объект (Factory), 'приложение' => объект (приложение), 'ошибки' => объект (ViewErrorBag), 'пользователь' => объект (пользователь)), 'obLevel' => '1', '__env' => объект (фабрика), 'приложение' => объект (приложение), 'ошибки' => объект (ViewErrorBag), 'пользователь' => объект (пользователь), 'примечание' => true)) в 5da731c9bf1a02452259c329d3c273e0 строка 226

У меня отношения настроены так:

Модель пользователя:

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

Модель AdminNote:

...
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
...

дд($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
}

дд ($ примечание)

true

person RobDiablo    schedule 05.09.2015    source источник
comment
Может ли пользователь иметь более одной заметки администратора?   -  person Rwd    schedule 05.09.2015
comment
Да, их может быть больше одного.   -  person RobDiablo    schedule 05.09.2015
comment
Чувак, я только что ответил на свой вопрос, не так ли? Спасибо, что помогли указать на это. Уххх.   -  person RobDiablo    schedule 05.09.2015


Ответы (1)


Спасибо Россу Уилсону за указание на то, что мои отношения были настроены так, чтобы иметь только одну заметку, тогда как их нужно было настроить так, чтобы было много заметок.

...
class User extends Model
{
  ...
  public function adminNotes()
  {
    return $this->hasMany('sass\AdminNote');
  }
  ...
}
person RobDiablo    schedule 05.09.2015