组中的 UserMiddleware 问题

UserMiddleware issue in groups

我正在开发一个 Laravel 5.1 应用程序,用户可以在其中创建群组并通过邀请码邀请成员加入该群组。 我已经能够为所有者制作中间件,但我在使用 UserMiddleware 时遇到问题。

这个中间件的目的是确保只有用户才能浏览他们所属的组。

这是我目前拥有的代码:

我的数据库关系:

// User.php

// User that creates the group, is owner of this group (seperate middleware)
public function ownedGroups() 
{
    return $this->hasMany('App\Group', 'owner');
}
// Users can be member of more then one group
public function groups()
{
    return $this->belongsToMany('App\Group');
}

// Group.php

//Owner connection in group
public function owner()
{
    return $this->belongsTo('App\User');
}
// Users are member of this group
public function users()
{
    return $this->belongsToMany('App\User');
}

我的迁移:

    Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('username')->unique();
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->string('avatar');
            $table->string('name');
            $table->string('lastname');
            $table->string('country');
            $table->string('vocation');
            $table->string('twitter');
            $table->string('facebook');
            $table->rememberToken();
            $table->timestamps();
        });

    Schema::create('groups', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            // User id gets set as an owner (user that creates the group)
            $table->integer('owner');
            $table->string('shortdesc');
            $table->string('longdesc');
            $table->string('groupimage');
            $table->string('invitecode');
            $table->string('slug')->unique();
            $table->timestamps();
        });

    // Pivot table to connect the two tables
    Schema::create('group_user', function(Blueprint $table)
        {
            $table->integer('group_id')->unsigned()->index();
            $table->foreign('group_id')->references('id')->on('groups')->onDelete('cascade');

            $table->integer('user_id')->unsigned()->index();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

            $table->timestamps();
        });

我的路线:

# Group
    $router->get('groups', ['as' => 'group', 'uses' => 'GroupController@index']);
    $router->get('groups/create', ['as' => 'group.create', 'uses' => 'GroupController@create']);
    $router->get('groups/{slug}', ['as' => 'group.show', 'uses' => 'GroupController@show']);
    $router->post('groups', ['as' => 'group.store', 'uses' => 'GroupController@store']);
    $router->post('groups/join', ['as' => 'group.join', 'uses' => 'GroupController@joinGroup']);
    $router->patch('groups/{slug}', ['as' => 'group.updateInvite', 'uses' => 'GroupController@updateInviteCode']);

我的所有者中间件: 因此,例如,我将 ownermiddleware 添加到 'updateInviteCode' 函数中。

public function handle($request, Closure $next)
    {
        $group = Group::whereSlug($request->slug)->first();
        $owner = $group->owner;
            if ($owner !== Auth::id()) 
            {
                return redirect('groups');
            }

        return $next($request);
    }
}

我现在的目的是制作一个用户中间件,这样只有成员才能进入他们的特定群组,而不能浏览他们不属于的群组..

目前我有一个查询,它给出了特定组中用户的集合,但是如何将登录用户与集合中给定的项目进行比较? 我想应该有某种循环,但我不知道如何进行。

public function handle($request, Closure $next)
        {
            $group = Group::whereSlug($request->slug)->first();
            $user->whereHas('groups', function($query) use ($group) {
    $query->where('id', '=', $group->id);
})->get();
                if ($user !== Auth::id()) 
                {
                    return redirect('groups');
                }

            return $next($request);
        }
    }

如有任何帮助,我们将不胜感激!

谢谢

我昨天自己找到了解决方案:

public function handle($request, Closure $next)
    {
        $group = Group::whereSlug($request->group->slug)->first();

        foreach ($group->users as $user)
        {
            if($user->id == Auth::id())
            {
                return $next($request);
            }
        }

    \Flash::error('You are not a user of this group!');
    return redirect('groups');
    }