2017-08-08 7 views
1

このようにすべてのビューで関数にアクセスできます。私はすべてのコントローラにアクセスすることができますどのように私のAppServiceProvideコードすべてのコントローラで関数にアクセスする方法laravel

public function boot() 
{ 
    $post = Post::latest()->first(); 
    View::share(compact('post')); 
} 

データベースから最新のレコードを確認する必要があるので、すべてのコントローラで関数を呼び出す最良の方法は何ですか。

+0

はまた、あなたはどこにでも –

+0

')('ます$ this->関数として他のコントローラでController.phpの利用機能で関数を定義します。 $ this-> function()の代わりに関数を呼び出すような方法はありますか? –

+0

はい、私はこれを行うことができます使用することができます静的クラス/シングルトンを作成することができます –

答えて

1
**You can write in model also using model object** 

public function YourFunction(){ 
    return "data"; 
} 

**call it in your controller like this make model object suppose your 
model name is YourModel in controller you can call like this** 

$model = new YourModel(); 
$data = $model->YourFunction(); //calling method in controller 
関連する問題