Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.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 雄辩不';我们不能回报它所期望的_Php_Laravel 4 - Fatal编程技术网

Php 雄辩不';我们不能回报它所期望的

Php 雄辩不';我们不能回报它所期望的,php,laravel-4,Php,Laravel 4,事实上,我有一个让我发疯的问题。 我是拉雷维尔的一个无名小卒,上周我做了一个芭蕾舞团,试着用真实的项目学习 我的用户和项目之间有一对多的关系(一个用户可以创建多个项目) 这是我的两款车型: class Project extends Eloquent { protected $table = 'projects'; protected $fillable = array('name', 'description'); public $timestamps = true

事实上,我有一个让我发疯的问题。 我是拉雷维尔的一个无名小卒,上周我做了一个芭蕾舞团,试着用真实的项目学习

我的用户和项目之间有一对多的关系(一个用户可以创建多个项目)

这是我的两款车型:

class Project extends Eloquent {

    protected $table = 'projects';
    protected $fillable = array('name', 'description');
    public  $timestamps = true; 

    public function user()
    {
        return $this->belongsTo('User');
    }
}
和我的用户模型:

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    protected $table = 'users';
    public  $timestamps = true;  
    protected $hidden = array('password', 'remember_token');

    public function tasks()
    {
        return $this->belongsToMany('Task');
    }

    public function projects()
    {
        return $this->hasMany('Project', 'created_by');
    }

}
然后,当我试图让我的项目显示他们与用户的数据链接到它,它给了我一些奇怪的东西。 以下是我的方法:

    public function lists()
    {
        $projects = Project::all();//with('user')->get();
        return compact('projects');
    }
当我这样做时,而不是用“with”请求加入时,它会给出(用var_dump)

我有我做的图图的例子,但我也做了同样的,它不起作用。 我希望得到这样的东西:

object(Post)[407]
  protected 'fillable' => 
    array (size=3)
      0 => string 'titre' (length=5)
      1 => string 'contenu' (length=7)
      2 => string 'user_id' (length=7)
  public 'timestamps' => boolean true
  protected 'connection' => null
  protected 'table' => null
  protected 'primaryKey' => string 'id' (length=2)
  protected 'perPage' => int 15
  public 'incrementing' => boolean true
  protected 'attributes' => 
    array (size=6)
      'id' => int 100
      'created_at' => string '2014-12-02 16:22:54' (length=19)
      'updated_at' => string '2014-12-02 16:22:54' (length=19)
      'titre' => string 'Titre99' (length=7)
      'contenu' => string 'Contenu 99 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud                     exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur                sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.' (length=492)
      'user_id' => int 8
  protected 'original' => 
    array (size=6)
      'id' => int 100
      'created_at' => string '2014-12-02 16:22:54' (length=19)
      'updated_at' => string '2014-12-02 16:22:54' (length=19)
      'titre' => string 'Titre99' (length=7)
      'contenu' => string 'Contenu 99 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud                     exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur                sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.' (length=492)
      'user_id' => int 8
  protected 'relations' => 
    array (size=2)
      'user' => 
        object(User)[420]
          protected 'table' => string 'users' (length=5)
          public 'timestamps' => boolean true
          protected 'hidden' => 
            array (size=2)
              ...
          protected 'connection' => null
          protected 'primaryKey' => string 'id' (length=2)
          protected 'perPage' => int 15
          public 'incrementing' => boolean true
          protected 'attributes' => 
            array (size=8)
              ...
          protected 'original' => 
            array (size=8)
              ...
          protected 'relations' => 
            array (size=0)
              ...
          protected 'visible' => 
            array (size=0)
              ...
          protected 'appends' => 
            array (size=0)
              ...
          protected 'fillable' => 
            array (size=0)
              ...
          protected 'guarded' => 
            array (size=1)
              ...
          protected 'dates' => 
            array (size=0)
              ...
          protected 'touches' => 
            array (size=0)
              ...
          protected 'observables' => 
            array (size=0)
              ...
          protected 'with' => 
            array (size=0)
              ...
          protected 'morphClass' => null
          public 'exists' => boolean true
      'tags' => 
        object(Illuminate\Database\Eloquent\Collection)[450]
          protected 'items' => 
            array (size=4)
              ...
  protected 'hidden' => 
    array (size=0)
      empty
  protected 'visible' => 
    array (size=0)
      empty
  protected 'appends' => 
    array (size=0)
      empty
  protected 'guarded' => 
    array (size=1)
      0 => string '*' (length=1)
  protected 'dates' => 
    array (size=0)
      empty
  protected 'touches' => 
    array (size=0)
      empty
  protected 'observables' => 
    array (size=0)
      empty
  protected 'with' => 
    array (size=0)
      empty
  protected 'morphClass' => null
  public 'exists' => boolean true

如果有人知道为什么,我会祝福你

您正在检索所有记录,这就是为什么您有一个[0],它是一个项目数组(集合)

尝试只转储一个项目

$projects = Project::all();
var_dump($projects->first());

不要var_dump收集对象,而是执行
var_dump($collection->toArray())
从收集中转储数据。。。。。但是你也在压缩yoru projects查询的结果,为什么?我在tuto中学到了这一点,当我转储相同类型的数据时,一个return me collection对象和正确的一个(有效的)返回给我一个正确对象的出现我想得到所有项目,目前我的数据库中只有一个,我知道你的意思,但是var_dump已经在foreach中了,但是当我执行->first()时,它给了我项目发生,但是在我的另一个项目中,当我执行相同操作时,我不需要使用->first()或类似的东西来获取我的对象。
$projects = Project::all();
var_dump($projects->first());