简体   繁体   English

laravel雄辩的关系无法访问媒体资源

[英]laravel Eloquent Relations can't access Property

I have got these three models with relations: 我得到了以下三种具有关系的模型:

class Comment 课程评论

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 类用户

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 类帖子

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');
  }
}

In controller Simply 在控制器中

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

view file: 查看文件:

@foreach($feed as $post)

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

In view file it returns that is undefined property if var_dump() ; 在视图文件中, if var_dump() ;则返回未定义的属性if var_dump() ; return only true but if I put this foreach loops in controller function and return value $comment->user->username , it return name of user so my question is what should I do to be able to use this property in view file 仅返回true,但是如果我将此foreach循环放入控制器函数中并返回值$comment->user->username ,则它返回用户的名称,因此我的问题是我应该怎么做才能在视图文件中使用此属性

Controller 控制者

<?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');
        }
    }




}

try to create dataCollection using function collect($yourArray) then send to view with "with" function 尝试使用collect($ yourArray)函数创建dataCollection,然后使用“ with”函数发送视图

i didnt using hasOne or anything, i'm using model::find($id) to search the same id 我没有使用hasOne或其他任何东西,而是在使用model :: find($ id)搜索相同的ID

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

that function is used to match the default format of laravel 该函数用于匹配laravel的默认格式

btw i'm using laravel 5.4 顺便说一句我正在使用laravel 5.4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM