Laravel Modeling以单行连接三个表

时间:2015-12-11 11:57:40

标签: laravel laravel-5

我有3个表/模型,例如UsersCurrentCurrencyCurrencyTypeCurrentCurrency 2列与CurrencyType和{{1}有关系},Usersuser_id

我可以使用此代码来抓取currency_id用户:

CurrentCurrency

此代码返回与用户的所有记录,现在我想通过建模创建与$all_records = CurrentCurrency::with('user')->orderBy('id', 'DESC')->paginate(50); 简单相关的内容,不幸的是,对于此表,我得到CurrencyType

CurrentCurrency:

null

CurrencyType:

class CurrentCurrency extends Model
{
    protected $table = 'current_currency';
    protected $fillable = ['currency_id', 'current_money', 'user_id'];
    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo('App\User');
    }

    public function currency_type()
    {
        return $this->belongsTo('App\CurrencyType');
    }
}

用户:

class CurrencyType extends Model
{
    protected $table = 'currency_type';
    protected $fillable = ['currency_type', 'currency_symbol', 'user_id'];

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

    public function currency()
    {
        return $this->hasMany('App\current_currency');
    }
}

通过此代码,我可以获得用户信息:

class User extends Model implements AuthenticatableContract,
    AuthorizableContract,
    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;

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

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    /**
     * @param $value
     */
    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = Hash::make($value);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function currentCurrency()
    {
        return $this->belongsToMany('User', 'CurrentCurrency', 'user_id', 'currency_id');
    }
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function currencyType()
    {
        return $this->hasMany('App\CurrencyType');
    }
}

但是我无法获得$all_records = CurrentCurrency::with(['user', 'currency_type'])->orderBy('id', 'DESC')->paginate(50); foreach ($all_records as $key => $contents) { echo $contents->user; } ,那会返回currency_type

enter image description here

结果:

null

2 个答案:

答案 0 :(得分:3)

您必须更新CurrentCurrency中的关系,如下所示:

return $this->belongsTo('App\CurrencyType','currency_id', 'id');
// where currency_id is foreign_key and id is otherKey referring to id of currency_type table 

同时更新您的查询以选择具有CurrencyType的用户,如下所示:

$all_records = CurrentCurrency::with(array('user','currencyType'))->orderBy('id', 'DESC')->paginate(50);

http://laravel.com/docs/5.1/eloquent-relationships#updating-belongs-to-relationships

答案 1 :(得分:0)

在档案 CurrencyType

return $this->hasMany('App\current_currency');

应该是

return $this->hasMany('App\CurrentCurrency');