0

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.

1 Answer 1

3

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()
Sign up to request clarification or add additional context in comments.

1 Comment

@Curtis no problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.