0

I've got a database, and in the database there are two tables called 'accounts' and 'players'. For the 'accounts' table I used the User.php model, and generated a new one for 'players' table. I've defined the relationships and everything.

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');
    }
}

Every player has a matching account_id column. I usually used Auth::user(), because all that I needed was my own account_id.

Now I have a third model, for a search form.

Search.php

    <?php

class Search extends Eloquent {


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

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

I think I've defined the relationship good on this one, if I did not, you could change it.

What I need is when I search the data, and retrieve the specific player, to display other players that have his account_id.

4

0 回答 0