AppServiceProvider启动功能和动态标题

i passed variable to all views in AppServiceProvider boot() i stored in this variable the name of the first user in the database so i can put it in my layout as title

the problem happenes when there is no user in the database (new databases) it through exception which says $title is not defind and that is true it is not defind so i tried to make if statment but it ignore it for some reason

boot ()

  public function boot()
    {
      Schema::defaultStringLength(191);

      View::composer('*',function($view){
        $title = DB::table('users')->first();
        $view->with('title',$title);
      });

i tried this as the logic but didn't work

 public function boot()
    {
      Schema::defaultStringLength(191);

      $check = DB::table('users')->first();
        if ($check != null) {
          View::composer('*',function($view){
            $title = DB::table('users')->first();
            $view->with('title',$title);  });
        }else {
          return view('nouser');
        }

there is no syntax error but i think the problem is every time the website reloads it renders all the layouts ( i have 3) guest and admin have

<title>{{$title->name}}</title>

and app which the title is not dynamic in it and the nouser page i coded i extended the app layout bcs it doesn't have this variable which is undefined

the problem is i need to hide this exception from the user and redirect him to page that says please register and then i redirect him to the main pages after regiestration

i solved it by doing it in my pages controller insted of the appservice provider

pages controller

public function gethome()
  {
    $title = DB::table('users')->first();
      if ($title == null) {
        return redirect('/register');
      }else{
        $cvg = CV::all();
        return view('guest.cv')->with('cvg', $cvg);
  }

and did it for all the guest directories which they are 4 main routes

in AppServiceProvider

public function boot()
    {
      Schema::defaultStringLength(191);

      View::composer('*',function($view){
      $title = DB::table('users')->first();
      $view->with('title',$title);
  });
    }

in my guest layout

<title>{{$title->name}}</title>

hope this help anyone have the same problem Good luck and have fun coding

The main problem on your code is; in case of there is no user you are trying to print something in the object. You should care S.O.L.I.D. principles when trying to implement something.

$countUsers = DB::table('users')->count();
$title = null;
if($countUsers > 0){
    $title = DB::table('users')->first()->name; 
}
View::composer('*',function($view){
    $view->with('title',$title);
});
if($countUsers == 0 ){
    return view('nouser');
}

and in your template use

<title>{{$title}}</title>

instead of

<title>{{$title->name}}</title>

Using View::composer is not a better solution to dynamically changing titles. So, you can use artesaos/seotools package for the dynamic titles, descriptions and much more easily. Also, it has got a dynamic twitter and opengraph meta generation feature which is really important for the seo.

<?php
....
class ForExampleController extends {
    public function viewUser($ID){
        $user = User::findOrFail($ID);
        SEO::setTitle($user->name);
        SEO::setDescription("This is the profile of ".$user->name);
        SEO::opengraph()->setUrl(...); // you can set users profile url
        // and much more..
    }
}
?>