Search code examples
laravel-5modelscontrollers

Calling a model function from a controller in Laravel 5.2


I have a model config which has the following at the top:

<?php

namespace App;

use DB;
use Illuminate\Database\Eloquent\Model;

class Config extends Model
{
    protected $table = 'config';

    public function getConfigVariables()
    {
        $config = DB::table('config')->where('is', '1')->first();
        session()->put('name',$config['name']);
        session()->put('infoemail',$config['infoemail']);
        session()->put('copyrightowner',$config['copyrightowner']);

and I wish to call this in a controller to set up the session so in the route for the top level I set set up

Route::get('/',
[
    'uses' => 'ConfigController@ConfigVariables',
    'as' => 'home'
]);

The config controller method which does not work is:

 public function ConfigVariables()
    {

            Config::getConfigVariables();
            session()->put('thisyear',ReturnCurrentYear());
            $footer = "&copy ".session()->get('thisyear').", ".session()->get('name');
            session()->put('footer',$footer);

        return view('welcome');
    }

but this does not work and I am stuck!


Solution

  • Change

     public function getConfigVariables()
    

    to

     public static function getConfigVariables()
    

    You might want to read up how object oriented works, basically when you do Config::getConfigVariables(); you are trying to call a static method, without instantiating the class.

    A good start would be here, the concept applies everywhere.