简体   繁体   中英

Laravel Eloquent not returning join table

I have the following function that joins 2 models.

$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.

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.

whereHas doesn't load relations. You should use with for this. Try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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