父母建造laravel

问题描述 投票:0回答:1

我想使用父构造来调用一次变量

class Home extends Controller {
    public function __construct(){
        $expiresAt = 10;
    }

    public function index() {

        $data = Cache::remember('table', $expiresAt, function() {
                return DB::table('table')
                        ->ORDERBY('date', 'DESC')
                        ->WHERE('status', 1)
                        ->paginate(8);
        });

        return view('frontend.pages.home', compact('title', 'meta', 'description', 'all_resto'));
    }
}

我有这个回答“Undefined variable:expiresAt”问题出在哪里

php laravel-5 controller
1个回答
0
投票
class Home extends Controller {
    public $expiresAt;
    public function __construct(){
        $this->expiresAt = 10; // change over here.
    }

    public function index() {

        $data = Cache::remember('table', $expiresAt, function() {
                return DB::table('table')
                        ->ORDERBY('date', 'DESC')
                        ->WHERE('status', 1)
                        ->paginate(8);
        });

        return view('frontend.pages.home', compact('title', 'meta', 'description', 'all_resto'));
    }
}

旁注:在扩展Home类的类中,您可以使用$expiresAt变量

$this->expiresAt

© www.soinside.com 2019 - 2024. All rights reserved.