0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravel すべてのviewで共通する変数の設定

Posted at

はじめに

Laravelを勉強中です。備忘録として残しておきます。

手順

サービスプロバイダーの作成

app/Provider/ComposerServiceProvider.php

<?php
namespace App\Providers;
 
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Auth;
 
class ComposerServiceProvider extends ServiceProvider
{
    public function boot()
    {
        View::composer('*', function($view) {
            $view->with('user'(使いたい変数名), Auth::user()(使いたいデータ));
        });
    }
}

View::composerの第一引数「*」はすべてのviewという意味。

作成したComposerServiceProviderの登録

config/app.php
    'providers' => [
        .
        .
        .
     App\Providers\ComposerServiceProvider::class,
    ],

これですべてのviewで使用できるようになります。

参考にさせて頂いたサイト

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?