Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel @stack in component

I've created a tile component in my Laravel app, which is used on several pages to display/link to recipes (note: this is not an angular, vue or react component).

In my tile component I "push" a css file to a stack called styles:

@push('styles')
  <link href="{{ mix('css/tile.css') }}" rel="stylesheet">
@endpush

In my head.blade.php the styles stack is outputted:

@stack('styles')

Every time my component is called/rendered, the tile.css file is added to my style stack. This works like a charm, my tiles are styled according to my tile.css file. The only problem is that the tile.css file is added to the styles stack multiple times.

Is there a way to prevent/check for double inserts in the styles stack, or do I have to manually add the tile.css file to every page/blade file on which the tile component is generated?

like image 877
Tomjesch Avatar asked Sep 14 '25 20:09

Tomjesch


1 Answers

pushonce was introduced in Laravel 7.

@pushonce('styles')
  <link href="{{ mix('css/tile.css') }}" rel="stylesheet">
@endpush
like image 186
ahinkle Avatar answered Sep 16 '25 09:09

ahinkle