Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Model from Controller - Laravel 4

I am a newbie to Laravel. Please excuse me if this question sounds kiddish.

I have a model

class Config extends Eloquent {

    public static $table = 'configs';

}

and the controller goes as

class UserController extends BaseController {

    Public function getIndex ()
    {
        $config_items = Config::all ();
        var_dump ( $config_items );
        return View::make ( 'user.userindex' )
                        -> with ( 'title', 'User Page' );
    }

}

But when i try to access the Config model, i am getting the error:

Symfony \ Component \ Debug \ Exception \ FatalErrorException Call to undefined method Illuminate\Config\Repository::all()

Error Message

Please help!

I know this question could help many Laravel 4 newbies like me and my co-workers, so please help!

like image 638
Guns Avatar asked Dec 12 '22 13:12

Guns


1 Answers

As the commenter pointed out, Config is actually a class that's already defined / used.

You have two options:

Option 1:

Namespace your Config model:

<?php namespace My\Models;

use Illuminate\Database\Eloquent\Model;

class Config extends Model { ... }

Then in your controller:

$config_items = My\Models\Config::all();

Note: If you go with option 1 (I suggest you do), you'll need to set up autoloading for your namespaced library. See this blog article on setting up your own Laravel library with autoloading.

Option 2:

Don't use Config as a model name:

<?php

class Configuration extends Eloquent { ... }

Then in your controller:

$config_items = Configuration::all();

Hope that helps!

like image 78
fideloper Avatar answered Dec 26 '22 06:12

fideloper