Php 返回NULL的关系
我对拉雷维尔很陌生。 我想显示所需的数据,并创建与表的关系,但它不起作用。你能找出我的错误吗 我有两个表用户和位置Php 返回NULL的关系,php,mysql,laravel,web,eloquent,Php,Mysql,Laravel,Web,Eloquent,我对拉雷维尔很陌生。 我想显示所需的数据,并创建与表的关系,但它不起作用。你能找出我的错误吗 我有两个表用户和位置 Collection {#121 ▼ #items: array:3 [▼ 0 => User {#322 ▶} 1 => User {#323 ▶} 2 => User {#324 ▼ #fillable: array:5 [▶] #hidden: array:2 [▶] #connection:
Collection {#121 ▼
#items: array:3 [▼
0 => User {#322 ▶}
1 => User {#323 ▶}
2 => User {#324 ▼
#fillable: array:5 [▶]
#hidden: array:2 [▶]
#connection: "mysql"
#table: "users"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [▶]
#original: array:10 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"Places" => null
]
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [▶]
#rememberTokenName: "remember_token"
}
]
}
用户模型
protected $fillable = array('name', 'email', 'password','role','places_id');
public function places(){
return $this->belongsTo('App\Places');
}
protected $fillable = array('name','email','phone','address','bio');
public function users()
{
return $this->hasMany('App\User');
}
放置模型
protected $fillable = array('name', 'email', 'password','role','places_id');
public function places(){
return $this->belongsTo('App\Places');
}
protected $fillable = array('name','email','phone','address','bio');
public function users()
{
return $this->hasMany('App\User');
}
控制器
$users = User::with('Places')->get();
return view('/dashboard')->with('users',$users);
输出为
Collection {#121 ▼
#items: array:3 [▼
0 => User {#322 ▶}
1 => User {#323 ▶}
2 => User {#324 ▼
#fillable: array:5 [▶]
#hidden: array:2 [▶]
#connection: "mysql"
#table: "users"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [▶]
#original: array:10 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"Places" => null
]
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [▶]
#rememberTokenName: "remember_token"
}
]
}
我想你已经交换了关系。反过来说: 用户模型:
public function places()
{
return $this->hasMany('App\Places');
}
public function places()
{
return $this->hasMany('App\Places', 'places_id', 'id');
}
用户拥有外键places\u id
,因此用户不属于某个位置,而是拥有一个位置
地点型号:
public function users()
{
return $this->belongsTo('App\User');
}
public function users()
{
return $this->belongsTo('App\User', 'places_id', 'id');
}
地点没有用户,而是属于用户。我知道一开始会让人困惑,但你一定会掌握窍门的!你可以用这个“把戏”来帮助你(至少我一直都是这么记得的):
模型“关系”的名称函数的名称
原始海报新注释后的添加/更新(带外键): 用户模型:
public function places()
{
return $this->hasMany('App\Places');
}
public function places()
{
return $this->hasMany('App\Places', 'places_id', 'id');
}
地点型号:
public function users()
{
return $this->belongsTo('App\User');
}
public function users()
{
return $this->belongsTo('App\User', 'places_id', 'id');
}
关系的格式是
('Model Name'、'foreign key'、'local key')
,那么到底是什么不起作用呢?从我的立场来看,一切似乎都很好。关系“Places”返回NULL,我希望它返回数据这给我一个错误SQLSTATE[42S22]:Column not found:1054未知列“Places.user_id”in“where子句”能否在原始问题中发布表结构?(只是为了完整性)。我认为如果在users()函数中以不同的方式命名某些列,则需要在Places模型中指定外键:return$this->belongsTo('App\User','Places\u id','id')
Places Table[id,name,email,phone,address,bio)Users Table[id,name,email,password,role,Places\u id)谢谢你提供的信息。我更新了我的答案。你能试着用一下吗?它奏效了!谢谢。虽然我没有为每个函数更改hasMany和belongs,但我只是添加了“外键”和“本地键”。非常感谢。