【发布时间】: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表中都可用。
-
添加有问题的用户模型代码。
-
你能准确说出是哪一行导致了错误吗?