Use of models on packaging

I am trying to use laravel packages. I created MyVendor / MyPackage

Routes, controllers, filters are already working. This is a cool map of my package:

"classmap": [
        "src/migrations",
        "src/controllers",
        "src/seeds",
        "src/models"
    ],

This is what my model looks like:

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

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

$test = new models\MyModel;      

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

I can’t understand why. I am new to namespaces, so maybe this is due to this. I tried with the composer update, the dump-autoload linker (inside my package) and still cannot find my models.

If I get declared classes using get_declared_classes (), I don't see my model there.

The problem is that my model classes are not autoload.

+4
2

:

  • models classmap.
  • YourModel.php :

    <?php
    // Note no namespace
    
    use \Illuminate\Database\Eloquent\Model as Eloquent;
    
    class YourModel extends Eloquent {
        //
    }
    
  • composer dump-autoload ,
  • , route.php:

    <?php
    $testModel = YourModel::get();
    die(var_dump($testModel));
    ?>
    
+7

Laravel 4.2

<?php namespace Vendor\Package;

use \Illuminate\Database\Eloquent\Model as Eloquent;

class Product extends Eloquent {

    ...

}
0

All Articles