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?
$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
Get the value like this