Laravel 关系 table 用户和地址
Laravel relation table user and address
我遇到了一个小问题,我尝试检索我与用户的 table 地址关系的数据。
在我的数据库地址中,我有一个外键 user_id 关联到用户 table ID。
我的模型用户
public function addresses()
{
return $this->belongsToMany('Address');
}
我的模特地址
public function user(){
return $this->hasOne('User');
}
我尝试在我的 UsersController 中检索我尝试在我的配置文件方法中显示用户的地址
控制器用户
public function profile($id){
if($id != Auth::user()->id){
return Redirect::to('logout');
}
$user=User::find(Auth::user()->id);
$address = $user->addresse;
dd($address);
$comments = Comment::where('user_id','=',Auth::user()->id)
->take(10)->get();
return View::make('users.profile')
->with(array('comments'=>$comments,'address'=>$address));
}
但在我看来我无法检索我的关系用户地址的数据。
profile.blade.php 查看
在您的 table 地址中删除 user_id 并在 table 用户中创建行名称 address_id.
改变
模范用户:
public function addresses()
{
return $this>belongsToMany('address_id','Address');
}
这个:
$地址 = $用户->地址:
为此:
$address = $user->Address->address(name of the row with the address);
好的,你有几个错误。首先你不需要再次定义 user 因为 Auth::user() 已经是你的用户模型了。所以你需要做的就是
$user = Auth::user();
之后
public function profil($id){
......
$user=Auth::user();
// this will bring users all related addresses as an array.
//so you can loop through and use them.
$adress = $user->adresses;
..........
}
如果您的目标是为每个用户创建一个地址记录,那么您应该对您的模型进行一些更改。
在您的用户模型中,只需添加一个 address_id 列并从地址 table 中删除 user_id 列。通过这种方式,您将拥有一对一的关系。每个用户将只有一个地址。
之后在您的用户模型中删除 addresses() 函数并添加地址(单数)函数
public function address(){
return $this->belongsTo("Address");
}
并在您的地址模型中
public function user(){
return $this->hasOne("User");
}
会给你你所需要的。然后在你的控制器中 class
public function profil($id){
......
$user=Auth::user();
$adress = $user->adress;
// will give you a single address record belongs to user.
..........
}
我遇到了一个小问题,我尝试检索我与用户的 table 地址关系的数据。
在我的数据库地址中,我有一个外键 user_id 关联到用户 table ID。
我的模型用户
public function addresses()
{
return $this->belongsToMany('Address');
}
我的模特地址
public function user(){
return $this->hasOne('User');
}
我尝试在我的 UsersController 中检索我尝试在我的配置文件方法中显示用户的地址 控制器用户
public function profile($id){
if($id != Auth::user()->id){
return Redirect::to('logout');
}
$user=User::find(Auth::user()->id);
$address = $user->addresse;
dd($address);
$comments = Comment::where('user_id','=',Auth::user()->id)
->take(10)->get();
return View::make('users.profile')
->with(array('comments'=>$comments,'address'=>$address));
}
但在我看来我无法检索我的关系用户地址的数据。
profile.blade.php 查看
在您的 table 地址中删除 user_id 并在 table 用户中创建行名称 address_id.
改变
模范用户:
public function addresses()
{
return $this>belongsToMany('address_id','Address');
}
这个: $地址 = $用户->地址:
为此:
$address = $user->Address->address(name of the row with the address);
好的,你有几个错误。首先你不需要再次定义 user 因为 Auth::user() 已经是你的用户模型了。所以你需要做的就是 $user = Auth::user();
之后
public function profil($id){
......
$user=Auth::user();
// this will bring users all related addresses as an array.
//so you can loop through and use them.
$adress = $user->adresses;
..........
}
如果您的目标是为每个用户创建一个地址记录,那么您应该对您的模型进行一些更改。
在您的用户模型中,只需添加一个 address_id 列并从地址 table 中删除 user_id 列。通过这种方式,您将拥有一对一的关系。每个用户将只有一个地址。
之后在您的用户模型中删除 addresses() 函数并添加地址(单数)函数
public function address(){
return $this->belongsTo("Address");
}
并在您的地址模型中
public function user(){
return $this->hasOne("User");
}
会给你你所需要的。然后在你的控制器中 class
public function profil($id){
......
$user=Auth::user();
$adress = $user->adress;
// will give you a single address record belongs to user.
..........
}