Php Laravel FollowController关系错误

Php Laravel FollowController关系错误,php,laravel,laravel-5,Php,Laravel,Laravel 5,我正在制作FollowController,其中在用户表和项目表之后有两个表。这有很多关系。当当前用户想要跟踪某个用户时,该用户的ID将存储在后续用户表中,其关系表存储当前用户ID和后续用户ID(即后续用户表的ID)。这是模式 以下用户表: public function up() { Schema::create('following_users', function (Blueprint $table) { $table->bigIncr

我正在制作
FollowController
,其中在用户表和项目表之后有两个表。这有很多关系。当当前用户想要跟踪某个用户时,该用户的ID将存储在后续用户表中,其关系表存储当前用户ID和
后续用户ID
(即后续用户表的ID)。这是模式

以下用户表

public function up()
    {
        Schema::create('following_users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('user_id')->unsigned();
            $table->foreign('user_id')
                  ->references('id')
                  ->on('users');
            $table->timestamps();
        });
    }
public function up()
    {
        Schema::create('following_user_items', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('following_users_id')->unsigned();
            $table->foreign('following_users_id')
                  ->references('id')
                  ->on('following_users');
            $table->bigInteger('user_id')->unsigned();
            $table->timestamps();
        });
    }
以下用户项目表

public function up()
    {
        Schema::create('following_users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('user_id')->unsigned();
            $table->foreign('user_id')
                  ->references('id')
                  ->on('users');
            $table->timestamps();
        });
    }
public function up()
    {
        Schema::create('following_user_items', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('following_users_id')->unsigned();
            $table->foreign('following_users_id')
                  ->references('id')
                  ->on('following_users');
            $table->bigInteger('user_id')->unsigned();
            $table->timestamps();
        });
    }
我已经做了
FollowController
,但问题是在尝试检查用户是否已经被跟踪时出现了

遵循
用户
模型中的关系

public function followingUserList()
    {
        return $this->hasOne('App\FollowingUser');
    }

    /**
     * Get the stories associated with the user through an intermediate table
     *
     */
    public function followingUsers()
    {
        return $this->hasManyThrough(
            'App\FollowingUserItem',
            'App\FollowingUser',
            null,
            'following_users_id'
        );
    }
followuser
User
followuseritem

public function user()
{
    return $this->belongsTo('App\User');
}


public function users()
{
    return $this->hasMany('App\FollowingUserItem','following_users_id');
}
这是我的
FollowController

class FollowController extends Controller
{
    //

    public function index($id)
    {
        $user = User::find($id);
        $logged_userId = Auth::User();

        if ($user->id == $logged_userId->id) {
            return [
                'status' => false,
                'message' => 'You can not Follow yourself',
            ];
        }
        if ($user && $logged_userId) {
            $checkUsers = FollowingUser::where('user_id', $user->id)->get()->users()->where('user_id', $logged_userId->id);
            if ($checkUsers) 
            {

                    return 'already followed';

            }
            else 
            {
                $user->followingUserList()->save(new FollowingUser())->users()->save(new FollowingUserItem(['user_id' => $logged_userId->id]));
                return 'sucess';
            }
        }
    }       
}
我承认错误 方法Illumb\Database\Eloquent\Collection::用户不存在。

当您调用
get()
Laravel时返回一个
集合,因为它不知道将有多少行。这就是为什么您得到的
集合
没有用户设置错误。由于您根据一个id进行筛选,您知道只有一个id,因此您可以使用
first()
方法

因此,将代码更改为使用
first()

调用
get()
Laravel时返回一个
集合
,因为它不知道将有多少行。这就是为什么您得到的
集合
没有用户设置错误。由于您根据一个id进行筛选,您知道只有一个id,因此您可以使用
first()
方法

因此,将代码更改为使用
first()