kankai
1 month ago
151
6
Laravel

How can i get last two query with relationship

Posted 1 month ago by kankai

Hi guys,

I'm trying to use eloquent to get last two rows of my data, but i can't figure out how to make it.

id | game_id | value
1  | abc          |10
2  | abc          | 23
3  | cds          | 21
4  | cds          | 44 

So what i need to return is Game ID 'abc' : value = 10 and 'cds' : value = 21

My controller

$doneData = oldwork::with('hasOneWallet','hasLastopup')->where('category', $cat)->where('status', 0)->get();

eloquent model

public function hasOneWallet(){
        return $this->hasOne('App\game_id','game_id','game_id'); 
 }

public function hasLastopup(){
        return $this->hasMany('App\game_id','game_id','game_id')->latest()->take(2); 
 }

I have tried hasLastopup() to get latest 2 result but it fail. Anyway to make this happen? Thanks

Please sign in or create an account to participate in this conversation.