我是新手,我正在学习laravel 5.2,我有我与laravel做一个博客,我有所谓的“设置”,在这里,数据库中的表是它的迁移文件:如何在Laravel 5.2中从数据库中获取特定数据的自定义配置文件?
public function up()
{
Schema::create('settings', function (Blueprint $table) {
$table->increments('id');
$table->string('site_name');
$table->text('site_description');
$table->string('about_head');
$table->text('about_description');
$table->text('about_body');
$table->timestamps();
});
}
我向这张表输入了数据,现在我想将这些数据传递给所有控制器和所有方法。 我发现最好的方法是在App \ Config文件夹上创建一个自定义配置文件。 所以我做了一个名为“定制”的文件,我试图访问数据库来获取从设置表中的数据和这里的自定义配置:
<?php
use Illuminate\Support\Facades\Config;
use App\Setting;
$setting = Setting::find(1);
return array(
'site_name' => $setting->site_name,
'site_description' => $setting->site_description,
'about_head' => $setting->about_head,
'about_description' => $setting->about_description,
'about_body' => $setting->about_body,
);
但我得到了我的laravel根目录错误
Fatal error: Call to a member function connection() on null in C:\xampp\htdocs\myblog\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php on line 3280
有什么想法如何解决它?或者至少你知道另一个解决方案来将我的网站设置数据传递给所有控制器,方法和视图吗?
谢谢你的时间。
里面'MyConfig'我想读这些值。 ) –
对不起,但我没有看到任何好的教程。人们说Laracasts有一些很好的相关课程,但它们不是免费的。您可以从了解服务提供商开始。 https://laravel.com/docs/master/providers和MyConfig只是一个简单逻辑的自定义类。 –
谢谢我在这个链接上找到答案[链接](http://stackoverflow.com/questions/28608527/how-to-pass-data-to-all-views-in-laravel-5) –