Laravel 4检索数据

时间:2013-08-14 13:49:54

标签: laravel laravel-4

我有一个数据库,在数据库中有两个名为'accounts'和'players'的表。 对于'accounts'表,我使用了User.php模型,并为'players'表生成了一个新的。我已经定义了关系和一切。

user.php的

    <?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    public static $rules = array(
        'name' => 'required|unique:accounts,name|min:4|AlphaNum',
        'email' => 'required|unique:accounts,email|email',
        'password' => 'required|min:4|AlphaNum',
        'password_confirm' => 'same:password'
        );

    public static $rules2 = array(
        'name'  => 'required|min:4|max:32',
        'password'  => 'required|min:4|max:255'
        );

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'accounts';

    /**
     * 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;
    }


    /**
     * hasMany Relationship
     *
     */
    public function players() {
        return $this->hasMany('Player','account_id');
    }

    /**
     * Check to see if the account is an administrator.
     *
     * @access public
     * @return boolean
     */
    public function isAdmin()
    {
        $player = Player::whereAccountId($this->id)->whereExists(function($query) {
            $query->select(\DB::raw(1))
                ->from('groups')
                ->where('groups.access', '>', 0)->where('groups.flags', '>', 0)
                ->orderBy('groups.access', 'DESC')->orderBy('groups.flags', 'DESC')
                ->whereRaw('groups.id = players.group_id');
        })->first();

        return (boolean) $player;
    }

}

Player.php

 <?php

class Player extends Eloquent {


    protected $table = 'players';

    protected $fillable = array('character_name', 'sex', 'vocation');
    public $timestamps = true;

    public function user() 
    {
        return $this->belongsTo('User');
    }
}

每个玩家都有匹配的account_id列。我通常使用Auth::user(),因为我需要的只是我自己的account_id。

现在我有第三个模型,用于搜索表单。

的search.php

    <?php

class Search extends Eloquent {


    protected $table = 'players';
    public $timestamps = false;

    public function user() 
    {
        return $this->belongsTo('User');
    }
}

我认为我已经在这个关系中定义了良好的关系,如果我没有,你可以改变它。

我需要的是搜索数据并检索特定播放器,以显示拥有其account_id的其他玩家。

0 个答案:

没有答案