$all = Sdc::with(['disciplineRelation:id,name'])
->whereHas('studentRelation', function ($query) use ($request) {
$query->where('id', $request->student);
})
->join('disciplines', 'sdcs.discipline_id', '=', 'disciplines.id')
->orderBy('disciplines.name') // Sort directly by discipline name
->distinct('disciplines.id') // Ensure uniqueness at the database level
->select('sdcs.*') // Select only columns from the main table
->get();
You should let the database do the sorting like @jsanwo64 shows, it's both faster and uses less resources that sorting a collection in php, just make sure that you have an index on the name column.
However, try not to use distinct in your query if you can avoid it, it generally slows the query down.