Home>
I am creating a simple SNS system with Laravel.

I got stuck when implementing the follow function for logged-in users.
Get the number of followers and the number of followers by the logged-in user.
I want to display the numbers in Layouts/Login.blade.php, which has a common layout.
I was able to get and display a variable in one particular child view, but when I go to another page I get an error.
(I know that I get an error because I haven't passed any variables to that page.)
I thought that it was inefficient to write the process for all pages, and after investigating various things, I arrived at the view composer and tried various things, but I am in trouble because the process cannot be passed well.
We would appreciate it if you could give us detailed advice on how to use the view composer and examples of how to use it.

Applicable source code FollowsController
namespace App \ Http \ Controllers;
use Illuminate \ Http \ Request;
use Illuminate \ Support \ Facades \ Auth;
use Illuminate \ Support \ Facades \ DB;
use App \ User;
use App \ Post;
use App \ Follow;
class FollowsController extends Controller
{
    public function show (User $user, Follow $follow)
    {
        $login_user = auth ()->user ();
        $is_following = $login_user->isFollowing ($login_user->id);
        $is_followed = $login_user->isFollowed ($login_user->id);
        $follow_count = $follow->getFollowCount ($login_user->id);
        $follower_count = $follow->getFollowerCount ($login_user->id);
        return view ('users.show', [
            'is_following' =>$is_following,
            'is_followed' =>$is_followed,
            'follow_count' =>$follow_count,
            'follower_count' =>$follower_count
        ]);
    }
}
Follow model
<? php
namespace App;
use Illuminate \ Database \ Eloquent \ Model;
class Follow extends Model
{
  protected $fillable = [
    'following_id','followed_id'
  ];;
  public function getFollowCount ($user_id)
  {
      return $this->where ('following_id', $user_id)->count ();
  }
  public function getFollowerCount ($user_id)
  {
      return $this->where ('followed_id', $user_id)->count ();}
}
User model
<? php
namespace App;
use Illuminate \ Notifications \ Notifiable;
use Illuminate \ Foundation \ Auth \ User as Authenticatable;
class User extends Authenticatable
{
    use Notifiable;
   /**
     * The attributes that are mass assignable.
     *
     * @var array
     * /
    protected $fillable = [
        'username','mail','password',
    ];;
   /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     * /
    protected $hidden = [
        'password','remember_token',
    ];;
    public function follow ($user_id)
    {
        return $this->follows ()->attach ($user_id);
    }
    public function unfollow ($user_id)
    {
        return $this->follows ()->detach ($user_id);
    }
    public function isFollowing ($user_id)
    {
        return (boolean) $this->follows ()->where ('followed_id', $user_id)->exists ();
    }
    public function isFollowed ($user_id)
    {
        return (boolean) $this->followers ()->where ('following_id', $user_id)->exists ();
    }
}


↓ The following files I want to use

AppServiceProvider
<? php
namespace App \ Providers;
use Illuminate \ Support \ ServiceProvider;
use Illuminate \ Support \ Facades \ View;use App \ User;
use App \ Follow;

class AppServiceProvider extends ServiceProvider
{
   /**
     * Bootstrap any application services.
     *
     * @return void
     * /
    public function boot ()
    {
        // //
        View :: composer (
          '*','App \ Http \ Composers \ ViewComposer');
}
   /**
     * Register any application services.
     *
     * @return void
     * /
    public function register ()
    {
        // //
    }
}
View Composer
<? php
namespace App \ Http \ Composers;
use Illuminate \ View \ View;
use App \ User;
use App \ Follow;
use Auth;
class View Composer
{
   /**
     * Bootstrap the application services.
     * @param View $view
     * @return void
     * /

    public function compose ($view)
    {
        // //
    }
}


In the process of FollowsController, a variable is passed to a specific page and the correct numerical value can be obtained.
I want View Composer to do the same and display the numbers.
Please give me some advice!