Laravel injects variables into layout view problem
大家讲道理
大家讲道理 2017-05-16 16:50:22
0
2
381

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?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
Peter_Zhu

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

为情所困

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template