Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 拉威尔雄辩的关系可以';t访问属性_Php_Laravel_Eloquent - Fatal编程技术网

Php 拉威尔雄辩的关系可以';t访问属性

Php 拉威尔雄辩的关系可以';t访问属性,php,laravel,eloquent,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->be

我得到了这三个关系模型:

课堂评论

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

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 Comment extends Eloquente {

  use UserTrait, RemindableTrait;

  protected $table = 'comments';


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

  public function post()
  {
    return $this->belongsTo('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');
  }
}
简单地在控制器中

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




}

尝试使用函数collect($yourray)创建数据收集,然后使用“with”函数发送到视图

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

例如: $dataCollection=collect($array1)

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


顺便说一句,我使用的是laravel 5.4

是否有可能某些评论没有指定用户?试试:
{($comment->user?$comment->user->username:“”)}
不,数据库中有一条注释,并且已经分配了用户,当我从控制器打印这个时,它会输出用户名OK。您能否将在控制器中工作的代码添加到问题中?在控制器中,您返回(第一个)
$comment->user->username
。我怀疑这个循环会运行一次以上,导致在第二次、第三次迭代中出现错误。。。如果你用
echo$comment->user->username
而不是
return
,你会出错吗?哦,顺便说一句,我很确定你的
评论
Post
模型不需要
UserTrait
提醒trait
。这些仅适用于用户模型。。。