Where do you perform this query? It looks like it's in one of your relations. Can you show those?
Feb 27, 2018
7
Level 1
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
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
Please or to participate in this conversation.