Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rjin's avatar
Level 1

Laravel Query Builder get error

I posted this question on laravel.io as well:

I'm following the Laravel from Scratch series from laracasts and am trying to build my own website.

I have a method in Posts that allows me to establish the relationship between comments and post

public function comments()
{
    return $this->hasMany(Comment::class);
}

Let's say I have an integer valued attribute, attr, in Comments and I want to perform a query to see all the Comments that have an attr value of greater than 0. I created a new method in Posts that checks this:

public function greaterThanZero()
{
    $query = $this->comments->where('attr', '>', 0);
    dd($query->get());
            ...
}

For some reason I'm getting this error:

Type error: Too few arguments to function Illuminate\Support\Collection::get(), 0 passed in ../project/app/Post.php on line 29 and at least 1 expected (View: ../project/resources/views/posts/show.blade.php)

Yet when I remove ->get() , I can see an array of the comments that satisfy the query. I was wondering why does get() method fail here?

EDIT: Another question I had: what is the best way to query an instance of a model? Or should all queries be done with the generic model class? For example, here I want to query the specific post to query the comments associated with that post.

0 likes
2 replies
tykus's avatar

You are working with a Collection instance after $this->comments, if you want a Query Builder, then use the method:

$query = $this->comments()->where('attr', '>', 0);

// ...

The Collection instance has it's own where method which is, in your case, is filtering all comments on the Post.

Jaytee's avatar

There are a few things i would change here. But let's tackle your issues from top to bottom:

In cases where you want to continue filtering (such as checking if an attribute is greater than 0), you should call the relationship as a method rather than a property.

Check this out:

$this->comments // returns a collection of comments. Think of Laravel calling the get() method behind the scenes
$this->comments() // returns an instance of the query builder and the relationship itself. You are then responsible for calling get(), but it's much more flexible

So changing this:

$this->comments->where('attr', '>' 0);

To this:

$this->comments()->where('attr', '>', 0)->get();

Will work as you're accessing an instance of the query builder, in other words, it hasn't finished querying until you say it has (by calling the get method).

If that's a little bit confusing, think of it like this:

$this->comments returns a collection of comments, and you can go on with your day such as looping through them

$this->comments() allows you to continue adding constraints to the query such as checking if a value is greater than X or whatever you wish

Okay now for your second question:

You'll generally keep it within the respective models. If it's a complicated query etc, you could extract it to another class.

Now, here's a tip you can use to improve your code:

  • You could look into query scopes, this will often prove useful in a lot of situations where you need to filter results
3 likes

Please or to participate in this conversation.