kirobo
kirobo

Reputation: 313

hasMany Relation Laravel - Not getting query result

I have a post with multiple images. But I want to only fetch images with PNG format. So I do this is post :

 public function images() {
        return $this->hasMany('App\ImagesModel','mapID','id');
    }

    public function oftype($query)
    {
        return
        $query->where('type', '=', 'PNG');
    }

But this is not working.

$post = Post::find($id);
$post->images()->oftype()->get();

Kindly help.

Upvotes: 0

Views: 48

Answers (1)

Ian
Ian

Reputation: 3676

For that to work you need to use scopes

public function scopeType($query)
{
    return $query->where('type', '=', 'PNG');
}

Then use as

$post->images()->type()->get()

Upvotes: 2

Related Questions