binggle's avatar

'whereHas' just return the last record..

I want to retrive all Events records with belongsTo relationship ( Type )

But it gives me just one last Event record .

Can someone help me ?

in Controller .

$events = Event::where('weekday', $weekday)
            ->whereHas('type', function(  $q) use($type) {
                 $q->where('key',  $type);
            })->get();

Event.php

class Event extends Model {


    function type(){
        return $this->belongsTo('App\Type');   
    }
    

}

Type.php

class BetType extends Model {
        
    function events(){
        return $this->hasMany( App\Event::class);
    }

}

TIA

0 likes
2 replies
bobbybouwmann's avatar
Level 88

The query looks good to me, it seems that you just have one result. Also note that it only returns events if there is actually a type with that key.

You can replace get in your query with toSql and it should show you what query will be performed. You can try that on your database and see if that returns more results.

binggle's avatar

Sorry guys..

it was mistake..

Don't mind this question.

Please or to participate in this conversation.