简体   繁体   English

SQLSTATE [23000]:完整性约束违规:1052. Laravel eloquent 连接表时出现问题

[英]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.因为usersnews表都包含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.

相关问题 SQLSTATE [23000]:违反完整性约束:1052与laravel结合使用 - SQLSTATE[23000]: Integrity constraint violation: 1052 using join with laravel Laravel Eloquent SQLSTATE[23000]:违反完整性约束:1052 列...在 where 子句中不明确 - Laravel Eloquent SQLSTATE[23000]: Integrity constraint violation: 1052 Column ... in where clause is ambiguous SQLSTATE[23000]:完整性约束违规:1052 - SQLSTATE[23000]: Integrity constraint violation: 1052 Laravel 6 错误:SQLSTATE[23000]:违反完整性约束:1052 where 子句中的列“id_perusahaan”不明确 - Laravel 6 Error : SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id_perusahaan' in where clause is ambiguous 错误:违反完整性约束:加入 Laravel Eloquent 时出现 1052 - Error: Integrity constraint violation: 1052 when Join in Laravel Eloquent SQLSTATE [23000]:完整性约束违规:1052 order order中的'created_at'列不明确Laravel 5.5 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in order clause is ambiguous Laravel 5.5 SQLSTATE [23000]:完整性约束违规:1052 列“created_at”在 order 子句中不明确 Laravel 8 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created_at' in order clause is ambiguous Laravel 8 Laravel 5:SQLSTATE [23000]:违反完整性约束 - Laravel 5: SQLSTATE[23000]: Integrity constraint violation SQLSTATE [23000]:违反完整性约束:在Laravel 5上 - SQLSTATE[23000]: Integrity constraint violation: on Laravel 5 laravel 8 播种,SQLSTATE[23000]:违反完整性约束: - laravel 8 seeding , SQLSTATE[23000]: Integrity constraint violation:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM