Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Using models on packages

I'm trying to use Laravel packages. I created MyVendor/MyPackage

Routes, controllers, filters are already working. This is the classmap of my package:

"classmap": [

This is how looks my model:

namespace MyVendor\MyPackage\Models;             
class MyModel extends \Illuminate\Database\Eloquent\Model {   

And this is the code inside my controller which is in namespace MyVendor\MyPackage.

$test = new models\MyModel;      

I'm getting this error: Class 'MyVendor\MyPackage\models\MyModel' not found

I can't figure out why. I'm new with namespaces so maybe it is something related to this. I tried with composer update, composer dump-autoload (inside my package) and still can't find my models.

If I get the declared classes with get_declared_classes() I can't see my model there.

The problem is that my model classes are not autoloading.

like image 215
Carlos Goce Avatar asked Mar 22 '23 05:03

Carlos Goce

1 Answers

Try this:

  1. Create models directory inside your package and add it to the package's classmap
  2. Add a model YourModel.php with the following:

    // Note no namespace
    use \Illuminate\Database\Eloquent\Model as Eloquent;
    class YourModel extends Eloquent {
  3. Run composer dump-autoload from your package directory first and then root directory
  4. Test your model by putting this at the top of your routes.php file:

    $testModel = YourModel::get();
like image 119
Uze Avatar answered Apr 12 '23 17:04