search

Home  >  Q&A  >  body text

How to pass data to template page in laravel

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?

为情所困为情所困2749 days ago456

reply all(2)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-16 16:53:29

    <?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()
        {
            //
        }
    }
    

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-16 16:53:29

    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

    reply
    0
  • Cancelreply