Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

anin123's avatar

Inner join query in laravel

Hello help to create this sql query in laravel, thank you

UPDATE students s1 JOIN students s2 ON s1.studentID = ? AND s2.studentID = ?
   SET s1.class = ?, s2.class = ? WHERE s1.id = ? AND c2.id = ?

I have tried this:

DB::table('students')
            ->join('students as s1', 's1.studentID', '=', $studentID1)
            ->join('students as s2', 's2.studentID', '=', $studentID2)
            ->where(['s1.id' => $id, 's2.id' => $id2])
            ->update(['s1.class' => $class, 's2.class' => $class]);
0 likes
9 replies
Sinnbeck's avatar

Normally in a join you use ON to link columns between the two tables. Not sure what yours is doing? Why do you even need to join the table to itself

anin123's avatar

@Sinnbeck I tried to check user's selected students and update both students class to same variable.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@anin123 so just this?

DB::table('students')
->whereIn('id', [$id, $id2])
->update(['class' => $class]);
Sinnbeck's avatar

@anin123 what does this give you?

$s = DB::table('students')
->whereIn('id', [$id, $id2])
->get()->toArray();
dd($a);
anin123's avatar

@Sinnbeck

^ array:2 [▼
  0 => {#263 ▼
    +"id": 161
    +"studentID": 1
    +"class": 6
    +"additional_details": "111"
    +"deleted": 0
  }
  1 => {#268 ▼
    +"id": 162
    +"studentID": 2
    +"class": 2
    +"additional_details": null
    +"deleted": 0
  }
]

Please or to participate in this conversation.