Laravel 4查询显示
我有点小问题 我的数据库中有两个表:第一个是“accounts”,第二个是“players”。我已经做了和模特的关系,我已经把一切都做好了。但是,有些事我还没有做 在“accounts”表中有一个名为“id”的列。在“玩家”表中有一个名为“帐户id”的列,它与“帐户”表中的“id”匹配 我想显示,比如说球员姓名(“球员”表中的“姓名”列)。比如说,当一个人登录(“帐户”表)时,我如何显示球员的姓名(“球员”表中的“姓名”列),但要使用匹配的ID 例如: “账户” id=1;name=12345 “玩家” id=1;姓名=约翰;账户id=1 现在,当我登录到id为1的帐户(“accounts”表)时,我希望它显示account_id=1的所有玩家。我还想让它能够处理所有帐户/ID,而不仅仅是一个帐户 我已经尝试过这样做:Laravel 4查询显示,laravel,laravel-4,Laravel,Laravel 4,我有点小问题 我的数据库中有两个表:第一个是“accounts”,第二个是“players”。我已经做了和模特的关系,我已经把一切都做好了。但是,有些事我还没有做 在“accounts”表中有一个名为“id”的列。在“玩家”表中有一个名为“帐户id”的列,它与“帐户”表中的“id”匹配 我想显示,比如说球员姓名(“球员”表中的“姓名”列)。比如说,当一个人登录(“帐户”表)时,我如何显示球员的姓名(“球员”表中的“姓名”列),但要使用匹配的ID 例如: “账户” id=1;name=12345
public function player()
{
$player = Player::find(3);
return View::make('aac.test')->with('player',$player);
}
它只适用于一个id为3的玩家,与帐户id不匹配,它只显示id为3的玩家
还有我的User.php(model;用于“帐户”)和Player.php(model;用于“玩家”)
User.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');
}
}
只需获得所需帐户,即可访问玩家收藏。然后您只需在视图中迭代account->players
public function players()
{
$account = User::find(1);
return View::make('aac.test')->with('account',$account);
}
在您的视图中,您可以遍历玩家
<?php foreach($account->palyers as $player):?>
<?php echo $player->name;?>
<?php endforeach;?>
希望这有帮助它实际上帮助很大。我得到一个错误:异常SQLSTATE[42S22]:未找到列:“where子句”(SQL:select*fromplayers
whereplayers
user\u id
in(?),绑定:数组(0=>1,1=>15,)您的关系与用户模型有关,因此按照惯例,它会尝试查找user\u id
,而不是account\u id
。尝试在关系return$this->belongsTo('User','account_id')中添加自定义密钥
抱歉,在Player
modelreturn$this->hasMany('Player','account_id')
中尝试另一种方法。它现在可以工作了,但我得到了另一个错误:Symfony\Component\Debug\Exception\FatalErrorException调用非对象上的成员函数getAndResetWheres()。是否有方法getAndResetWheres()
您正在尝试呼叫的?还是来自框架?
<?php foreach($account->palyers as $player):?>
<?php echo $player->name;?>
<?php endforeach;?>