显示laravel 4.2中连接表的输出


show output of joined table in laravel 4.2

2所以我有两个名为usertype和user的表

usertype表有以下列

  • usertypeID
  • usertype

用户有这些列

  • userID
  • 用户名
  • usertypeID (usertype表中的外键)
  • userdescription

我在如何显示用户表(包括其相应的用户类型)方面遇到了麻烦。对于usertype和user

,我已经有了两个不同的模型下面是usertype 的表
class userstype extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'dbo_systemtypes';
/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('password', 'remember_token');

}

这是我的用户模型

class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'dbo_systemusers';
/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('password', 'remember_token');

}

以后再说,不过也许对别人有用。

给你的用户模型添加:

public function userType() {
    return $this->hasOne('UsersType', 'usertypeID');  // rename your userstype model to UsersType
}

然后得到所有用户属性,包括usertype:

$user = User::find(1);
$name = $user->userName;
$description = $user->userDescription;
$userType = $user->userType->usertype;
                //    ^           ^
                //dyn.property   column

或者如果你只想要usertype:

$userType = User::find(1)->userType->usertype;