简体   繁体   English

Laravel Eloquent不返回联接表

[英]Laravel Eloquent not returning join table

I have the following function that joins 2 models. 我有以下加入2个模型的函数。

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

I have a Game model that has the following code. 我有一个包含以下代码的游戏模型。

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

I also have a GameTransaction model that has the following code. 我还有一个GameTransaction模型,其中包含以下代码。

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

The problem I'm having is the $listing variable is only returning data from the game_listings table, and nothing from the game_transactions table. 我遇到的问题是$listing变量仅从game_listings表中返回数据,而从game_transactions表中没有返回数据。

whereHas doesn't load relations. whereHas没有加载关系。 You should use with for this. 您应该使用with此。 Try: 尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM