Support the ongoing development of Laravel.io →
Eloquent IOC

I'm trying to access a Model and Controller from a custom library class.

I have created an app/lib folder and added the folder to composer.json so that it is included in the autoload.

I have created a custom class -- RefreshWork. Within the RefreshWork class, I want to access some Models and Controllers in the main application. The script keeps bombing out when trying to access the Model (I haven't even gotten to the point of trying access a controller).

Here is the code contained in the app/lib/RefreshWork.php file:

class RefreshWork extends Stackable{
    
    public function __construct($module) {
        $this->module = $module;
    }
    
    /**
     * Does the work for refreshing the module
     */
    public function run() {
        
        $module = $this->module;
        
        $connection = Connection::find($module->connection_id); // <-- Script dies at this point.
    }
}

I thought this would be a simple task, and I don't know if the "extends Stackable" portion is what is causing the problem or not.

Any ideas?

Thanks, CJ

Last updated 3 years ago.
0

If you are inject module to your class, where are you storing it? I see you are calling

$this->module

but there's no 'module' property in you class, maybe you are just missing?

protected $module;

Also, are you trying to use Laravel's automatic dependency injection? Because if you are, how is Laravel supposed to know what to inject into the constructor? You need to specify what you expect to be injected, for example:

public function __construct(Module $module)

That way Laravel knows what you need and injects the instance for you.

Hope this helps

P.S: In your run function you don't need to assign the module property to a local variable so you could just run

$connection = Connection::find($this->module->connection_id);

Given that your class has a $module property like I pointed before.

Last updated 3 years ago.
0

After some deep diving, I believe the issue is around a previous call of $worker->start();.

I'm trying to implement pthreads in Laravel and it looks like the $worker->start(); call kills all of the model/controller functionality throughout the application.

I'll have to dig into this some more.

Thanks for the tips on better formatting of the Class delcaration, though.

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.