How to pass data to template page in laravel
为情所困
为情所困 2017-05-16 16:51:29
0
2
454

Front-end template layout.blade.php page part code:

<nav>{{ $message }}</nav>
<p class="container">  @yield('content') </p>
<footer></footer>

Other pages inherit this page.

How to pass the message data in nav from the background? Is it passed once in the controller of each content page? This seems very troublesome; another question, how to use Auth::user()->id in the boot() method of AppServiceProvider to obtain the id of the currently logged in user in various ways?

为情所困
为情所困

reply all(2)
淡淡烟草味
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Auth;

class ComposerServiceProvider extends ServiceProvider
{
    /**
     * 在容器中注册绑定.
     *
     * @return void
     * @author http://laravelacademy.org
     */
    public function boot()
    {

        // 使用基于闭包的composers...
        view()->composer('admin.header', function ($view) { 
            $data['order-remind']=null;
            $data['order']=null;
            $data['order-remind']=null;
            $view->with('data',$data);
        });

        view()->composer('admin.nav', function ($view) {
            $user=auth()->guard('admin')->user();
            $view->with('user',$user);
        });

        view()->composer('agent.main', function ($view) {
            $user=auth()->guard('agent')->user();
            $view->with('user',$user);
        });

        view()->composer('account.main', function ($view) {
            $user=auth()->guard('account')->user();
            $wechat=Auth::guard('wechat')->user();
            $view->with('account',$user)->with('wechatAccount',$wechat);
        });

        view()->composer(['wechat.js.index','wechat.activity.vote.main'], function ($view) {
            $app=app('wechat');
            $js=$app->js;
            $view->with('js',$js);
        });

    }

    /**
     * 注册服务提供者.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
伊谢尔伦
  1. $message You can directly write php statement query in {{}} of blade template. For example:
    Message::first() or Message::where()... and the like.

  2. The ID of the currently logged in user can also be written directly in the blade

    Auth::user()->id
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template