Hsmith1947's avatar

Laravel Users follow

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


I am trying user to user relationship

0 likes
7 replies
bobbybouwmann's avatar

Where do you perform this query? It looks like it's in one of your relations. Can you show those?

Hsmith1947's avatar

Here in 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']);
    }
Hsmith1947's avatar
Hsmith1947
OP
Best Answer
Level 1

I got the error it should be like this in User model

 public function isFollowing($userId) 
    {
        return (boolean) $this->follows()->where('follows_id', $userId)->first();
    }
1 like
OleksandrZnaidiuk's avatar
return (boolean) $this->follows()
                     ->where('follows_id', $userId)
                         ->first(['id']); // Here is a problem

You perform SQL query from 2 tables: 'users' and 'followers' via join, so you need specify from whom you want 'id' field.

Try it out:

return (boolean) $this->follows()->where('follows_id', $userId)->first(['users.id']);

Also if you leave 'first' method without arguments you code should work fine, isn't?

->first();
theborycuas's avatar

hi Hsmith1947 Could you solve the error that gave you in the controller to the memory to call the functions is isFollowing and unfollow? I'm stuck like you were two years ago, one hand please.

Please or to participate in this conversation.