You have a College with many Student rows and want to detach every student, without deleting them. Detaching means setting college_id to null.
One query on the relationship
$college->students()->update(['college_id' => null]);
students() (with parentheses) returns the relation query, so update() runs a single SQL statement:
update "students" set "college_id" = null where "students"."college_id" = 1
It returns the number of rows changed. Students of other colleges are untouched, and nothing is loaded into memory.
The column must be nullable:
$table->foreignId('college_id')->nullable()->constrained()->nullOnDelete();
nullOnDelete() does the same detaching at the database level when the college itself is deleted.
What a mass update skips
A mass update does not load models, so Eloquent events (updating, updated) and observers do not fire, and updated_at is not touched unless you set it yourself:
$college->students()->update(['college_id' => null, 'updated_at' => now()]);
If you need events, loop over the models instead. It costs one query per student:
$college->students->each(function ($student) {
$student->college_id = null;
$student->save();
});
Do not call $college->students()->delete(). That deletes the students.
Tested on Laravel 10.x (PHP 8.1) and 12.x (PHP 8.3): the tests count queries (one update), check that other colleges are untouched, and check that events fire in the loop but not in the mass update. Source: tests/laravel-verification/files/q3985.