简体   繁体   中英

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. 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. Can someone know what are the problem of my query? Help will be appreciated. Thanks

Index 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.

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

$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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM