I have this error
SQLSTATE[23000]: Integrity constraint violation: 1052 Column ‘id’ in field list is ambiguous (SQL: select id, followers.user_id as pivot_user_id, followers.follows_id as pivot_follows_id, followers.created_at as pivot_created_at, followers.updated_at as pivot_updated_at from users inner join followers on users.id = followers.follows_id where followers.user_id = 1 and follows_id = 2 limit 1)
(View: /Users/harshitsingh/Documents/logos/resources/views/users/index.blade.php) and this is my UsersController
<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppUser; use Image; use Auth; use Profile; use AppPost; use AppNotificationsUserFollowed; 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')); } }
I am trying user to user relationship this is User Model `
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']); }
`
Advertisement
Answer
Try to learn how to read the error message.
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous
This message tells you that there’s 2 or more id
field, which SQL doesn’t know which one to SELECT
. From your UserController
code, the code doesn’t seem to be in there, chances are you need to look at your User
model.
You will have to update
SELECT id ...
to
SELECT users.id ...