Waleed Ahmad
Waleed Ahmad

Reputation: 2294

Lumen: Using Eloquent in Lumen Project

I'm using Eloquent for my Lumen Project, After uncommenting

$app->withEloquent();

in bootstrap/app.php file, I've created database/models folder in my project for Model classes. Whenever i add a class in database/models, and try to use it, it throws and class not found exception, but after running composer dump-autoload, the class is available for use. How can i automate this, like just add the class model in database/models and use it, like we do in Laravel.

Here's my Model class.

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model{

    protected $table = 'posts';

    protected $fillable = array(
        'id',
        'p_id',
        'username',
        'title',
        'uri',
        'source',
        'points',
        'status',
        'language',
        'type',
    );
}

Here's my controller where i'm trying to use this model.

<?php namespace App\Http\Controllers;

use App\Post;

class ContentController extends BaseController{
    public function posts(Request $request){
        return Post::all();
    }
}

and here's my composer.json file

{
    "name": "laravel/lumen",
    "description": "The Laravel Lumen Framework.",
    "keywords": ["framework", "laravel", "lumen"],
    "license": "MIT",
    "type": "project",
    "require": {
        "laravel/lumen-framework": "5.1.*",
        "vlucas/phpdotenv": "~1.0",
        "illuminate/mail": "5.1.*"
    },
    "require-dev": {
        "phpunit/phpunit": "~4.0",
        "fzaninotto/faker": "~1.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "classmap": [
            "database/",
            "database/models"
        ]
    },
    "autoload-dev": {
        "classmap": [
            "tests/"
        ]
    },
    "config": {
        "preferred-install": "dist"
    }
}

Upvotes: 0

Views: 4077

Answers (3)

Cacing69
Cacing69

Reputation: 21

i think you should keep model in app\Models if u wanna try to move bas Model folder, just try to add folder in autoload part of PSR-4 and specify namespace for them

{
"name": "laravel/lumen",
"description": "The Laravel Lumen Framework.",
"keywords": ["framework", "laravel", "lumen"],
"license": "MIT",
"type": "project",
"require": {
    "laravel/lumen-framework": "5.1.*",
    "vlucas/phpdotenv": "~1.0",
    "illuminate/mail": "5.1.*"
},
"require-dev": {
    "phpunit/phpunit": "~4.0",
    "fzaninotto/faker": "~1.0"
},
"autoload": {
    "psr-4": {
        "App\\": "app/"
    },
    "classmap": [
        "database/",
        "database/models"
    ]
},
"autoload-dev": {
    "classmap": [
        "tests/"
    ]
},
"config": {
    "preferred-install": "dist"
}
}

to

{
"name": "laravel/lumen",
"description": "The Laravel Lumen Framework.",
"keywords": ["framework", "laravel", "lumen"],
"license": "MIT",
"type": "project",
"require": {
    "laravel/lumen-framework": "5.1.*",
    "vlucas/phpdotenv": "~1.0",
    "illuminate/mail": "5.1.*"
},
"require-dev": {
    "phpunit/phpunit": "~4.0",
    "fzaninotto/faker": "~1.0"
},
"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Models\\:"database/models"
    },
    "classmap": [
        "database/",
    ]
},
"autoload-dev": {
    "classmap": [
        "tests/"
    ]
},
"config": {
    "preferred-install": "dist"
}
}

Upvotes: 0

darthmaim
darthmaim

Reputation: 5158

Move your models from database/models to app/models and change the namespaces to App\Models and everything should work.

When using composers classmap to find classes, composer only caches the the paths when running composer dump-autoload. By using PSR-4 classloading, composer can find the file by looking at its namespace. Your App namespace is configured to be in the path app, and composer starts looking for them there.

Upvotes: 2

Nicolas Beauvais
Nicolas Beauvais

Reputation: 1052

You should keep your models in the app/ directory which as automatic PSR-4 class autoload thanks to the base lumen composer.json

Upvotes: 0

Related Questions