【问题标题】:Laravel 4 query displayingLaravel 4 查询显示
【发布时间】: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');
    }
}

【问题讨论】:

    标签: laravel laravel-4


    【解决方案1】:

    只需获得您需要的帐户,您就可以访问球员收藏。然后,您只需在视图中遍历 account-&gt;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;?> 
    

    希望对你有帮助

    【讨论】:

    • 它实际上有很大帮助。这是我得到的错误: Exception SQLSTATE[42S22]: Column not found: 1054 Unknown column 'players.user_id' in 'where clause' (SQL: select * from players where players.user_id in (?, ?)) (绑定: 数组 (0 => 1, 1 => 15, ))
    • 你的关系是与用户模型的关系,所以按照惯例它会尝试找到user_id而不是account_id。尝试在return $this-&gt;belongsTo('User', 'account_id'); 关系中添加您的自定义键
    • 抱歉,在Player 模型return $this-&gt;hasMany('Player','account_id') 中尝试相反的方式。
    • 它现在可以工作了,但是我得到另一个错误: Symfony \ Component \ Debug \ Exception \ FatalErrorException Call to a member function getAndResetWheres() on a non-object
    • 您是否有要调用的方法getAndResetWheres()?还是来自框架?
    猜你喜欢
    • 2014-08-11
    • 1970-01-01
    • 2014-11-02
    • 2015-02-13
    • 1970-01-01
    • 2013-06-19
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多