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

fogley's avatar

Cannot call custom Job method withing job itself

I have created a Job containing a few helper methods, contained within the job class itself. When I try to call the methods ($this->method($params);), I get an error that the method does not exist.

Any ideas?

0 likes
5 replies
Borisu's avatar

It depends on the context. Can you post your full job class?

fogley's avatar
fogley
OP
Best Answer
Level 1

Turns out I was using the exists() method on an already declared DB collection. So, I ended up using the first() method instead and treating the returned data as a boolean.

Is this an oversight? Why can't I use the exists() method on existing collections?

Borisu's avatar

@fogley Well it's unclear exactly what you did since you posted no code.

fogley's avatar

This is the gist of it:

$db_data = Model::all();
if($db_data->where('column', 'value')->exists()){
    // do stuff
}

Using first() instead of exists() solved the problem.

Borisu's avatar

@fogley Yeah there's no method exists on the collection class. You can use count which will return zero or more, it's actually what people use. I think the collection class is macroable so you can just extend it and have the method exists ;)

1 like

Please or to participate in this conversation.