Caution: Translations of Quran and Ḥadīth may lead to possible misapplications and misinterpretations. This site is intended for students of sacred knowledge that are proficient in comprehending classical Arabic and have a strong foundation in Islamic sciences. Also note that religious injunctions rely on several aspects beyond what one may glean through reading individual aḥādīth.
quran:18:21

And similarly, We caused them to be found that they ˹who found them˺ would know that the promise of Allah is truth and that of the Hour there is no doubt. ˹That was˺ when they disputed among themselves about their affair and ˹then˺ said, "Construct over them a structure. Their Lord is most knowing about them." Said those who prevailed in the matter, "We will surely take ˹for ourselves˺ over them a masjid."  

And so, just as We aroused them, it was that We disclosed them, ˹to˺ their people and the believers, that they, that is, their people, might know that God’s promise, of resurrection, is true: by virtue of the fact that One Who has the power to make them sleep for ˹such˺ a long period of time, or sustain them in that state without nourishment, also has the power to resurrect the dead; and that, as for the Hour, there is no doubt, no uncertainty, concerning it. Behold (idh adverbially qualifies a‘tharnā, ‘We disclosed’) they were disputing, that is, the believers and the disbelievers, among themselves their affair, the affair of the youths, with regard to building something around them ˹as a monument˺; so they, the disbelievers, said, ‘Build over them, that is, around them, a building, to cover them up; their Lord knows them best.’ Those who prevailed regarding their affair, the affair of the youths, namely, the believers, ‘We will verily set up over them, around them, a place of worship’, for prayers to be performed therein. And this indeed took place at the entrance of the cave.
القرآن:١٨:٢١

وَكَذَٰلِكَ أَعْثَرْنَا عَلَيْهِمْ لِيَعْلَمُوا أَنَّ وَعْدَ اللَّهِ حَقٌّ وَأَنَّ السَّاعَةَ لَا رَيْبَ فِيهَا إِذْ يَتَنَازَعُونَ بَيْنَهُمْ أَمْرَهُمْ ۖ فَقَالُوا ابْنُوا عَلَيْهِمْ بُنْيَانًا ۖ رَبُّهُمْ أَعْلَمُ بِهِمْ ۚ قَالَ الَّذِينَ غَلَبُوا عَلَىٰ أَمْرِهِمْ لَنَتَّخِذَنَّ عَلَيْهِمْ مَسْجِدًا  

{وكذلك} كما بعثناهم {أعثرنا} أطلعنا {عليهم} قومهم والمؤمنين {ليعلموا} أي قومهم {أن وعد الله} بالبعث {حق} بطريق أن القادر على إنامتهم المدة الطويلة وإبقائهم على حالهم بلا غذاء قادر على إحياء الموتى {وأن الساعة لا ريب} لا شك {فيها إذ} معمول لأعثرنا {يتنازعون} أي المؤمنون والكفار {بينهم أمرهم} أمر الفتية في البناء حولهم {فقالوا} أي الكفار {ابنوا عليهم} أي حولهم {بنيانا} يسترهم. {ربهم أعلم بهم قال الذين غلبوا على أمرهم} أمر الفتية وهم المؤمنون {لنتخذن عليهم} حولهم {مسجدا} يصلى فيه، وفعل ذلك على باب الكهف.