laravel Eloquent Relations无法访问Property

时间:2015-01-18 21:17:47

标签: php laravel eloquent

我有三个关系模型:

课程评论

class Comment extends Eloquente {

  use UserTrait, RemindableTrait;

  protected $table = 'comments';


  public function user()
  {
    return $this->belongsTo ('User');
  }

  public function post()
  {
    return $this->belongsTo('Post');
  }
}

类用户

class User extends Eloquent  {
  use UserTrait, RemindableTrait;

  /**
  * The database table used by the model.
  *
  * @var string
  */
  protected $table = 'users';

  /**
  * The attributes excluded from the model's JSON form.
  *
  * @var array
  */
  protected $hidden = array('password', 'remember_token');

  public function post()
  {
    return $this->hasMany('Post');
  }

  public function comment()
  {
    return $this->hasMany('Comment');
  }
}

课程

class Post extends Eloquent {
  use UserTrait, RemindableTrait;

  /**
   * The database table used by the model.
   *
   * @var string
   */
  protected $table = 'posts';

  /**
   * The attributes excluded from the model's JSON form.
   *
   * @var array
  */
  protected $hidden = array('password', 'remember_token');


  public function comment()
  {
    return $this->hasMany('Comment');
  }
  public function user()
  {
    return $this->belongsTo('User');
  }
}

在控制器中简单

return View::make('viewfile',['feed'=> Post::all()]);

查看文件:

@foreach($feed as $post)

     @foreach($post->comment as $comment)
        {{$comment->user->username}}
     @endforeach
@endforeach

在视图文件中,它返回未定义的属性 if var_dump() ;仅返回true 但是如果我把这个foreach循环放在控制器函数中 返回值$comment->user->username,返回用户名 所以我的问题是我该怎么做才能使用这个属性 在视图文件

控制器

<?php

class BoardController extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        if(Auth::check())
        {
            $followers_count = sizeof(unserialize(Auth::user()->Followers));
            $following_count = sizeof(unserialize(Auth::user()->Following));
            $following_groups_count = sizeof(unserialize(Auth::user()->GroupsFollowing));
            $feed = Post::all();

            foreach ($feed as $post) {

                foreach($post->comment as $comment)
                {
                    return $comment->user->username;
                }
            }


             /*

            return View::make('board.index',[
                    'FollowersCount' => $followers_count,
                    'FollowingCount' => $following_count,
                    'FollowingGroupsCount' => $following_groups_count,
                    'feed' => $feed,
                    'nick' => Auth::user()->username
                ]);
            */
        }
        else
        {
            Redirect::to('/login');
        }
    }




}

1 个答案:

答案 0 :(得分:-1)

尝试使用函数collect($ yourArray)创建dataCollection,然后使用&#34;发送到带有&#34;的视图。功能

我没有使用hasOne或其他任何东西,我使用model :: find($ id)来搜索相同的id

例如: $ dataCollection = collect($ array1);

该函数用于匹配laravel的默认格式

顺便说一句,我正在使用laravel 5.4