【问题标题】:Laravel FollowController relationship errorLaravel FollowController 关系错误
【发布时间】:2019-12-11 05:56:35
【问题描述】:

我正在制作FollowController,其中有两个表 following_users 表和 following_user_item 表。这是在 hasMany 关系中。当经过身份验证的 current_user 想要关注用户时,用户的 ID 将存储在 following_users 表中,其关系表存储 current_user_id 和 following_user_id(即 following_users 表的 id)。这是架构。

following_users_table:

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();
        });
    }

following_user_item_table:

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,但是在尝试检查用户是否已被关注时出现了问题。

关注User模型中的关系

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'
        );
    }

FollowingUserUserFollowingUserItem 的模型关系

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';
            }
        }
    }       
}

我犯了错误 方法 Illuminate\Database\Eloquent\Collection::users 不存在。

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    当你调用get() 时,Laravel 返回一个collection,因为它不知道那里会有多少行。这就是为什么您会收到 collection 没有用户设置错误的原因。由于您过滤了一个您知道只有一个的 id,因此您可以使用 first() 方法。

    所以将代码更改为使用first()

    $checkUsers = FollowingUser::where('user_id', $user->id)->first()->users()->where('user_id', $logged_userId->id);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-16
      • 2016-11-24
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      • 2018-10-21
      • 2020-05-02
      相关资源
      最近更新 更多