Support the ongoing development of Laravel.io →
Views Blade

Hey, I've an idea that I want to add modal structures through includes in my app. My problem now is that a modal can be added from several pages in the app, and there is a chance of "overlapping" modals. I mean that the modal can be added more than once because of the page structure.

I want to avoid the duplicate includes in the HTML structure some how, but allow for multiple @include for a certain modal, to make sure it's added. Is there any way of achieving this?

So far I have been without luck.

Last updated 3 years ago.
0
Last updated 9 years ago.
0

I had same issue. What slillo recommended is over-complicated in my opinion. Just declare a variable in an Util class.

Add this to your Util.php

public static $isReportModalIncluded = false;

then in your blade do this

@php

	//include this only once
	if (\App\Classes\Utils::$isReportModalIncluded === true) {
		return;
	}
	
	if (\App\Classes\Utils::$isReportModalIncluded === false) {
		\App\Classes\Utils::$isReportModalIncluded = true;
	}

	
@endphp

Hopefully Laravel will introduce an @includeonce in the future

Last updated 7 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

agoransson agoransson Joined 24 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.