【发布时间】:2013-07-24 22:58:20
【问题描述】:
我有一个小问题;
我的数据库中有两个表:第一个是“accounts”,第二个是“players”。我已经完成了与模型的关系,我已经完成了所有工作。但是,有些事情我没有做过。
在“帐户”表中有一个名为“id”的列。在“players”表中有一个名为“account_id”的列,它与“accounts”表中的“id”相匹配。
我想显示玩家名称(位于“玩家”表中的“名称”列)。比如说,当一个人登录('accounts' 表)时,我如何显示玩家的姓名(位于 'players' 表中的列 'name')但具有匹配的 id?
示例: '帐户' ID = 1;名称 = 12345 “玩家” ID = 1;姓名=约翰; account_id = 1;
现在,当我登录 id 为 1 的帐户('accounts' 表)时,我希望它显示所有 account_id = 1 的玩家。我还想让它适用于所有帐户/id,不仅仅是一个帐户。
我已经尝试过这样做:
public function player()
{
$player = Player::find(3);
return View::make('aac.test')->with('player',$player);
}
只针对id为3的玩家,与account_id无关,只显示id为3的玩家。
还有我的 User.php(模型;用于“帐户”)和 Player.php(模型;用于“玩家”)
用户.php
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'accounts';
/**
*
*
* Timestamps: true if needed, otherwise false.
*
*/
public $timestamps = false;
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password');
/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the e-mail address where password reminders are sent.
*
* @return string
*/
public function getReminderEmail()
{
return $this->email;
}
public function players() {
return $this->hasMany('Player');
}
}
播放器.php
<?php
class Player extends Eloquent {
protected $table = 'players';
protected $fillable = array('char_name', 'sex');
public $timestamps = false;
public function user() {
return $this->belongsTo('User');
}
}
【问题讨论】: