2017-04-04 08:09
浏览 112


I just want to Fetch Title and Tagline of the site from options table of my Database.
I tried Fetching it from AppServiceProvider.php but it shows Error that table does not Exist when I delete all the tables and run PHP artisan migrate.

I also tried Session but Finally, it takes me to the old AppServiceProvider.php method. It works fine but not when we install the Application fresh and run migration!

This is how my AppServiceProvider.php Looks like to get Idea what Exactly I want to do.

public function boot()
    $site_options = array();

    $site_title = Options::where('option_name','site_title')->first();
    $site_options['site_title'] = $site_title?$site_title->option_value:'Site Title';

    $site_tagline = Options::where('option_name','site_tagline')->first();
    $site_options['site_tagline'] = $site_tagline?$site_tagline->option_value:'A Simple website!';


I am searching for the answer on Stackoverflow and everywhere but I don't know what to do. I can also Create constructor on all the Controllers and Fetch the Data but then I have to pass the variables with every view and that makes the code look Bad!

Any Standard Solution for this?

PS: Seeding Does not work when I've this code Added into the AppServiceProvider.php nor Migration works. I have to Remove this code from AppServiceProvider.php in order to migrate and seed!

图片转代码服务由CSDN问答提供 功能建议

我只想从我的数据库的 options 表中获取网站的标题和标语 。 我尝试从 AppServiceProvider.php 中获取它,但是当我删除所有表并运行 PHP artisan migrate 时,它显示Error表不存在。

我也尝试了Session但最后,我需要使用旧的 AppServiceProvider.php 方法。 它工作得很好,但是当我们安装Application并运行迁移时却没有!

这就是我的 AppServiceProvider.php 看起来想知道我想要做什么。

  public function boot()
 $ site_options = array(); 
 $ site_title = Options :: where('option_name','site_title') - &gt  ; first(); 
 $ site_options ['site_title'] = $ site_title?$ site_title-> option_value:'Site Title'; 
 $ site_tagline = Options :: where('option_name','site_tagline')  - > first(); 
 $ site_options ['site_tagline'] = $ site_tagline?$ site_tagline-> option_value:'一个简单的网站!'; 
 view() - > share('site_options',  $ site_options); 

我在Stackoverflow上搜索答案,但我不知道该怎么办。 我也可以在所有控制器上创建构造函数并获取数据但是我必须在每个视图中传递变量,这会使代码看起来很糟糕!


PS:播种当我将此代码添加到 AppServiceProvider.php 中时,也不起作用。 我必须从 AppServiceProvider.php 中删除此代码才能迁移和播种!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doumao1887 2017-04-04 08:13

    Can you use a try catch on it?

    try {
        $site_title = Options::where('option_name','site_title')->first();
        $site_options['site_title'] = $site_title->option_value;
    } catch (\Exception $e) {
        $site_options['site_title'] = 'Site Title';
    解决 无用
    打赏 举报

相关推荐 更多相似问题