[英]Eloquent ORM relationships
我有两种模式:帐户:
<?php
namespace App\Models;
class Account extends \Illuminate\Database\Eloquent\Model
{
public function user()
{
return $this->belongsTo('\App\Models\User');
}
}
..和用户:
<?php
namespace App\Models;
class User extends \Illuminate\Database\Eloquent\Model
{
public function accounts()
{
return $this->hasMany('\App\Models\Account');
}
}
现在,当我执行以下操作时var_dump(\\App\\Models\\User::find(1)->accounts());
-我取回一个Illuminate \\ Database \\ Eloquent \\ Relations \\ HasMany实例,但是该实例不包含该用户ID的帐户。 我可以确认我的帐户表确实有几行,其中user_id设置为1。我在做错什么吗? 我遵循了文档,看不到任何不同之处。
下面是var_dump(\\App\\Models\\User::find(1)->accounts());
如果有用:
object(Illuminate\Database\Eloquent\Relations\HasMany)[67] protected 'foreignKey' => string 'accounts.user_id' (length=16) protected 'localKey' => string 'id' (length=2) protected 'query' =>
object(Illuminate\Database\Eloquent\Builder)[71]
protected 'query' =>
object(Illuminate\Database\Query\Builder)[72]
protected 'connection' =>
object(Illuminate\Database\MySqlConnection)[59]
...
protected 'grammar' =>
object(Illuminate\Database\Query\Grammars\MySqlGrammar)[61]
...
protected 'processor' =>
object(Illuminate\Database\Query\Processors\MySqlProcessor)[62]
...
protected 'bindings' =>
array (size=5)
...
public 'aggregate' => null
public 'columns' => null
public 'distinct' => boolean false
public 'from' => string 'accounts' (length=8)
public 'joins' => null
public 'wheres' =>
array (size=2)
...
public 'groups' => null
public 'havings' => null
public 'orders' => null
public 'limit' => null
public 'offset' => null
public 'unions' => null
public 'lock' => null
protected 'backups' =>
array (size=0)
...
protected 'cacheKey' => null
protected 'cacheMinutes' => null
protected 'cacheTags' => null
protected 'cacheDriver' => null
protected 'operators' =>
array (size=19)
...
protected 'model' =>
object(App\Models\Account)[57]
protected 'fillable' =>
array (size=3)
...
protected 'connection' => null
protected 'table' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean true
protected 'attributes' =>
array (size=0)
...
protected 'original' =>
array (size=0)
...
protected 'relations' =>
array (size=0)
...
protected 'hidden' =>
array (size=0)
...
protected 'visible' =>
array (size=0)
...
protected 'appends' =>
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 false
protected 'forceDeleting' => boolean false
protected 'eagerLoad' =>
array (size=0)
empty
protected 'macros' =>
array (size=4)
'forceDelete' =>
object(Closure)[70]
...
'restore' =>
object(Closure)[69]
...
'withTrashed' =>
object(Closure)[68]
...
'onlyTrashed' =>
object(Closure)[66]
...
protected 'onDelete' =>
object(Closure)[74]
protected 'passthru' =>
array (size=12)
0 => string 'toSql' (length=5)
1 => string 'lists' (length=5)
2 => string 'insert' (length=6)
3 => string 'insertGetId' (length=11)
4 => string 'pluck' (length=5)
5 => string 'count' (length=5)
6 => string 'min' (length=3)
7 => string 'max' (length=3)
8 => string 'avg' (length=3)
9 => string 'sum' (length=3)
10 => string 'exists' (length=6)
11 => string 'getBindings' (length=11) protected 'parent' =>
object(App\Models\User)[73]
protected 'fillable' =>
array (size=5)
0 => string 'name' (length=4)
1 => string 'username' (length=8)
2 => string 'email' (length=5)
3 => string 'password' (length=8)
4 => string 'salt' (length=4)
protected 'connection' => null
protected 'table' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean true
protected 'attributes' =>
array (size=9)
'id' => string '1' (length=1)
'name' => string 'Martyn' (length=6)
'username' => string 'martyn' (length=6)
'email' => string 'martyn@yahoo.co.uk' (length=18)
'password' => string 'password' (length=8)
'salt' => string 'salt' (length=4)
'created_at' => string '0000-00-00 00:00:00' (length=19)
'updated_at' => string '0000-00-00 00:00:00' (length=19)
'deleted_at' => null
protected 'original' =>
array (size=9)
'id' => string '1' (length=1)
'name' => string 'Martyn' (length=6)
'username' => string 'martyn' (length=6)
'email' => string 'martyn@yahoo.co.uk' (length=18)
'password' => string 'password' (length=8)
'salt' => string 'salt' (length=4)
'created_at' => string '0000-00-00 00:00:00' (length=19)
'updated_at' => string '0000-00-00 00:00:00' (length=19)
'deleted_at' => null
protected 'relations' =>
array (size=0)
empty
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
protected 'forceDeleting' => boolean false protected 'related' =>
object(App\Models\Account)[57]
protected 'fillable' =>
array (size=3)
0 => string 'name' (length=4)
1 => string 'amount' (length=6)
2 => string 'user_id' (length=7)
protected 'connection' => null
protected 'table' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean true
protected 'attributes' =>
array (size=0)
empty
protected 'original' =>
array (size=0)
empty
protected 'relations' =>
array (size=0)
empty
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 false
protected 'forceDeleting' => boolean false
使用普通语法:
User::find(1)->accounts()->get()
或通过动态属性 :
User::find(1)->accounts
但是不要将两者混为一谈。 accounts()
只是查询构建器的hasMany关系。 Account的实际对象由get()
检索。 动态属性缩短了您需要为此任务编写的代码
出于好奇,为什么要显式写出完全限定的名称空间? 您可以只使用extends Eloquent
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.