【问题标题】:Calling a model function from a controller in Laravel 5.2从 Laravel 5.2 中的控制器调用模型函数
【发布时间】:2016-06-17 08:23:33
【问题描述】:

我有一个模型配置,顶部有以下内容:

<?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']);

我希望在控制器中调用它来设置会话,所以在我设置的顶级路由中

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

不起作用的配置控制器方法是:

 public function ConfigVariables()
    {

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

        return view('welcome');
    }

但这不起作用,我被卡住了!

【问题讨论】:

    标签: laravel-5 models controllers


    【解决方案1】:

    改变

     public function getConfigVariables()
    

     public static function getConfigVariables()
    

    您可能想了解面向对象的工作原理,基本上当您执行Config::getConfigVariables(); 时,您是在尝试调用静态方法,而不是实例化类。

    here 是一个好的开始,这个概念适用于任何地方。

    【讨论】:

    • 谢谢! Laravel 和我习惯的很不一样,但我到了那里......慢慢地
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 2012-11-20
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多