search

Home  >  Q&A  >  body text

Laravel injects variables into layout view problem

Using laravel 5.3, you need to inject some variables into the shared layout view app.blade.php,

Added the following code in the boot() method of the AppServiceProvider.php file:

    public function boot()
    {
        view()->composer('layouts/app', function ($view) {
            $siteInfo=SiteInfo::all();
            dd($siteInfo);
            $view->with('siteName',$siteInfo->name)   // 这是line 22
                ->with('siteKeywords',$siteInfo->keywords)
                ->with('siteDescription',$siteInfo->description);
        });
    }

The following error occurred:

    ErrorException in AppServiceProvider.php line 22:
    Undefined property: Illuminate\Database\Eloquent\Collection::$name (View: D:\wnmp\www\laravel-5-3-dev\resources\views\pages\index.blade.php)
The location of line

22 is commented in AppServiceProvider.php.

dd($siteInfo);The result is like this:

Can't you use an arrow to get the value of a set's attribute?

大家讲道理大家讲道理2827 days ago390

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-05-16 16:52:22

    $siteInfo is two-dimensional. You can use ->Array to convert it into an array to get the value

    reply
    0
  • 为情所困

    为情所困2017-05-16 16:52:22

    Obviously wrong. Suppose your siteinfo table is designed as follows
    id guid, name varchar(60), value varchar(60)
    For the website key_word, you should take the value of the value field of the record whose name field is keyword

    So you need

    $siteinfo = SiteInfo::all();
    $siteinfo->where('name', 'key_word')->value;
    

    Get the value like this

    reply
    0
  • Cancelreply