Laravel5.4を使用して開発しています。
全ページに表示するお知らせ通知のようなものをヘッダーに表示したいと考えて、
全てのViewで共通で使う値を生成して全てのviewに渡せるようにしたいと思ってます。
Middlewareを作成し、View::shareでできるかなと思ったのですが、
bladeの方で値が取れてないようです。
Bladeの記述を消すとエラーは出ないので、@noticesがbladeに渡せてないのではないかと思うのですが、
追加で必要な設定などはありますでしょうか?
そもそもView::shareの使い方が間違っていますでしょうか?
アドバイスいただけると幸いです。
**App\Http\Middleware\MakeNotices** namespace App\Http\Middleware; use Auth; use Illuminate\Support\Facades\View; use App\Notice; use Closure; class MakeNotices { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if(Auth::check()){ $notices = Notice::where('user_id', '=', Auth::id()) ->where('opened_at', '!=',null) ->orderBy('created_at','asc') ->get(); View::share('notices', $notices); // $request->merge(['notices' => $notices]); // } return $next($request); } }
**App\Http\kernel.php** protected $middleware = [ //省略 \App\Http\Middleware\MakeNotices::class, //追加
**headder.blade.php** //抜粋 @foreach( $notices as $notice ) <li> {{ $notice->created_at }} </li> @endforeach
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。