2017-08-14 67 views
0

我有以下連接2個模型的函數。Laravel Eloquent不返回連接表

$listing = Game::where('status', '2')->whereHas('transaction', function ($query) use($id) { 
    $query->where('status',1)->where('transaction_id', Crypt::decrypt($id)); 
})->get(); 

我有了下面的代碼遊戲模型。

protected $table = 'game_listings'; 
protected $primaryKey = 'id'; 
protected $fillable = ['user_id','asset_id','trade_id','market_name','name','picture','description','price','status','clicks']; 
protected $dates = ['deleted_at']; 

/* 
|-------------------------------------------------------------------------- 
| RELATIONS 
|-------------------------------------------------------------------------- 
*/ 
public function user() 
{ 
    return $this->belongsTo('App\Models\User'); 
} 

public function transaction() 
{ 
    return $this->hasOne('App\Models\GameTransaction','listing_id','id'); 
} 

我也有具有以下代碼GameTransaction模型。

protected $table = 'game_transactions'; 
protected $primaryKey = 'id'; 
protected $fillable = ['listing_id', 'transaction_id', 'payee_id']; 
protected $dates = ['deleted_at']; 

我遇到的問題是$listing變量只從game_transactions表返回從game_listings表中的數據,並沒有什麼。

回答

3

whereHas不加載關係。你應該爲此使用with。試試:

Game::where('status', '2')->whereHas('transaction', function ($query) use($id) { 
    $query->where('status',1)->where('transaction_id', Crypt::decrypt($id)); 
})->with('transaction')->get() 
+0

非常感謝!儘管你在'with'語句中有一點拼寫錯誤,它的工作很完美。 – Curtis

+0

@Curtis沒問題 –