【问题标题】:Laravel Users followLaravel 用户关注
【发布时间】:2018-02-28 05:57:30
【问题描述】:

我有这个错误

SQLSTATE[23000]:完整性约束违规:1052 字段列表中的列“id”不明确(SQL:选择 id,followers.user_id 作为 pivot_user_id,followers.follows_id 作为 pivot_follows_id,followers.created_at 作为 pivot_created_at,followers.updated_at 作为来自用户内部的 pivot_updated_at 在 users.id = followers.follows_id 上加入关注者,其中 followers.user_id = 1 和 follow_id = 2 限制 1)

(查看:/Users/harshitsingh/Documents/logos/resources/views/users/index.blade.php) 这是我的用户控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use Image;
use Auth;
use Profile;
use App\Post;
use App\Notifications\UserFollowed;

class UsersController extends Controller
{
    public function index()
    {
        $users = User::where('id', '!=', auth()->user()->id)->get();
        return view('users.index', compact('users'));
    }

    public function profile(){
        return view('profile');
    }


    public function update_avatar(Request $request){

            // Handle the user upload of avatar
        if($request->hasFile('avatar')){
            $avatar = $request->file('avatar');
            $filename = time() . '.' . $avatar->getClientOriginalExtension();
            Image::make($avatar)
            ->resize(300, 300)
            ->save( public_path('/uploads/avatars/' . $filename ) 
        );

            $user = Auth::user();
            $user->avatar = $filename;
            $user->save();
        }

        return redirect('/');

    }

    public function follow(User $user)
    {
        $follower = auth()->user();
        if ($follower->id == $user->id) {
            return back()->withError("You can't follow yourself");
        }
        if(!$follower->isFollowing($user->id)) {
            $follower->follow($user->id);

            // sending a notification
            $user->notify(new UserFollowed($follower));

            return back()->withSuccess("You are now friends with {$user->name}");
        }
        return back()->withError("You are already following {$user->name}");
    }

    public function unfollow(User $user)
    {
        $follower = auth()->user();
        if($follower->isFollowing($user->id)) {
            $follower->unfollow($user->id);
            return back()->withSuccess("You are no longer friends with {$user->name}");
        }
        return back()->withError("You are not following {$user->name}");
    }

    public function notifications()
    {
        return auth()->user()->unreadNotifications()->limit(5)->get()->toArray();
    }


    public function show(Post $post, $id)
    {
        $user = User::findOrFail($id);
        return view('user.profile', compact('user'));
    }
}

我正在尝试用户对用户的关系 这是用户模型 `

 public function followers() 
        {
            return $this->belongsToMany(self::class, 'followers', 'follows_id', 'user_id')
                        ->withTimestamps();
        }
    
        public function follows() 
        {
            return $this->belongsToMany(self::class, 'followers', 'user_id', 'follows_id')
                        ->withTimestamps();
        }
    
        public function follow($userId) 
        {
            $this->follows()->attach($userId);
            return $this;
        }
    
        public function unfollow($userId)
        {
            $this->follows()->detach($userId);
            return $this;
        }
    
        public function isFollowing($userId) 
        {
            return (boolean) $this->follows()->where('follows_id', $userId)->first(['id']);
        }

`

【问题讨论】:

  • 两个表相同的列名。使用 select table_name.column_name
  • @Bugfixer 在哪里?
  • 选择 id,followers.user_id 作为 pivot_user_id,followers.follows_id 作为 pivot_follows_id,followers.created_at 作为 pivot_created_at,followers.updated_at 作为 pivot_updated_at 从用户内部加入 users.id 上的关注者= followers.follows_id where followers.user_id = 1 and follow_id = 2 limit 1 检查粗体部分这个id在两个join表中都可用。
  • 添加有问题的用户模型代码。
  • 你能准确说出是哪一行导致了错误吗?

标签: php laravel


【解决方案1】:

尝试学习如何阅读错误信息。

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous

此消息告诉您有 2 个或更多 id 字段,SQL 不知道 SELECT 是哪一个。从您的UserController 代码中,该代码似乎不在其中,您可能需要查看您的User 模型。

你必须更新

SELECT id ...

SELECT users.id ...

【讨论】:

    【解决方案2】:

    我知道这有点晚了,但在用户模型中你可以更改此代码

    public function isFollowing($userId) 
    {
       return (boolean) $this->follows()->where('follows_id', $userId)->first(['id']);
    }
    

    public function isFollowing($userId)
    {
        return !is_null($this->follows()->where('follow_id', $userId)->first());
    }
    

    然后它应该一切正常!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-23
      • 2015-10-02
      • 2016-02-13
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 2014-08-08
      • 2016-08-22
      相关资源
      最近更新 更多