[英]SQLSTATE[23000]: Integrity constraint violation: 1052. Laravel eloquent issue when joining table
I have News table and the column in the blade table posted by
is empty but in my news table the column users_id
which is the posted by has a users_id
value.我有 News 表,并且刀片表中
posted by
的列是空的,但在我的新闻表中,发布者的列users_id
具有users_id
值。 I cant get the user who posted the specific news in specific school.我无法获取在特定学校发布特定新闻的用户。 I already have a working query for the news, but the problem is i cant join the
users
table where the name
of the user who posted the news is in there.我已经对新闻进行了有效查询,但问题是我无法加入发布新闻的用户
name
所在的users
表。 Can someone know what are the problem of my query?有人可以知道我的查询有什么问题吗? Help will be appreciated.
帮助将不胜感激。 Thanks
谢谢
Index controller索引 controller
public function index()
{
//testing query that returns an error
$userschool = Newsboard::select('users.name', 'news.school_id')
->join('users', 'users.id', '=', 'news.users_id')
->where('school_id', Auth::user()->school_id)->get();
//the query that i want still returns an error because of the auth
$postedby = DB::select(
"SELECT news.*, users.name as postedby from news
JOIN users on news.users_id = users.id
WHERE news.school_id AND news.active = 1 AND news.school_id = 'Auth::user()->school_id'");
//working queries and i dont know how to convert the $postedby query join to eloquent.
if (Auth::user()->role == 0) {
$news = Newsboard::where('active','=',1)->get();
} elseif (Auth::user()->role == 1 || Auth::user()->role == 5) {
$news = Newsboard::where('school_id', '=', Auth::user()->school_id )
->where('active','=',1)
->get();
} else {
$role = Auth::user()->role;
$news = Newsboard::where('school_id', '=', Auth::user()->school_id )
->where('status', '=', 1)
->where('active','=',1)
->whereRaw("group_id in('$role', '0')")
->get();
}
dd($userschool);
return view('admin.pages.news.index', [
'page_title' => $this->page_title,
'news' => $news,
'mnuname' => 'News',
]);
}
Because both users
and news
table contain school_id
column, so in where condition must has table
prefix.因为
users
和news
表都包含school_id
列,所以 where 条件中必须有table
前缀。
Please try:请试试:
$userschool = Newsboard::select('users.name', 'news.school_id')
->join('users', 'users.id', '=', 'news.users_id')
->where('news.school_id', Auth::user()->school_id)->get();
//or
//the query that i want still returns an error because of the auth
$postedby = DB::select("SELECT news.*, users.name as postedby from news
JOIN users on news.users_id = users.id
WHERE news.school_id AND news.active = 1 AND news.school_id = '".Auth::user()->school_id."'");
Set your Auth::user()->school_id as variable将您的 Auth::user()->school_id 设置为变量
$school_id = Auth::user()->school_id;
//the query that i want still returns an error because of the auth
$postedby = DB::select(
"SELECT news.*, users.name as postedby from news
JOIN users on news.users_id = users.id
WHERE news.school_id AND news.active = 1 AND news.school_id = '$school_id'");
Or this:或这个:
Newsboard::select('users.name', 'news.school_id')
->join('users', 'users.id', '=', 'news.users_id')
->where(['news.school_id' => $school_id])->get();
$school_id = Auth::user()->school_id;
$userschool = Newsboard::select('users.name', 'news.school_id')
->join('users', 'users.id', '=', 'news.users_id')
->where('school_id', $school_id)->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.