我实际上有一个让我发疯的问题。 我是laravel的一个菜鸟,我上周做了一个tuto,并尝试用真正的项目学习。
我的用户和项目之间有一对多关系(一个用户可以创建多个项目)
这是我的2个型号:
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');
}
当我这样做时,而不是使用''要进行连接(使用var_dump)
object(Illuminate\Database\Eloquent\Collection)[316]
protected 'items' =>
array (size=1)
0 =>
object(Project)[314]
protected 'table' => string 'projects' (length=8)
public 'timestamps' => boolean true
protected 'connection' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
protected 'attributes' =>
array (size=10)
...
protected 'original' =>
array (size=10)
...
protected 'relations' =>
array (size=0)
...
protected 'hidden' =>
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
我从tuto那里得到了我的例子,但是我做得非常激动,并且它不起作用。 我希望得到类似的东西:
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 :(得分:0)
您正在检索所有记录,这就是您拥有[0]的原因,它是项目的数组(集合)
尝试只转储一个项目
$projects = Project::all();
var_dump($projects->first());