TOPIC COLLECTION
Views.
A collection of 20 practical ideas to make everyday Laravel work a little smoother.
20 TIPS01$Loop Variable in ForeachInside of foreach loop, check if current entry is first/last by just using $loop variable.Views02You Can Use Blade to Generate More Than HtmlYou can use it to generate any dynamic string or file you want. For example, a shell script or a sitemap file.Views03Short Attribute Syntax for Blade ComponentsAvailable from Laravel 9.32. Current syntaxViews04Share One Variable with Multiple ViewsHave you ever needed to share one variable with multiple views in Laravel? Here's a simple solution for that.Views05Does View File Exist?You can check if View file exists before actually loading it. You can even load an array of views and only the first existing will be actually loaded.Views06Error Code Blade PagesIf you want to create a specific error page for some HTTP code, like 503 - just create a blade file with this code as filename, in…Views07View without ControllersIf you want route to just show a certain view, don't create a Controller method, just use Route::view() function.Views08Blade @AuthInstead of if-statement to check logged in user, use @auth directive. Typical wayViews09Two-level $Loop Variable in BladeIn Blade's foreach you can use $loop variable even in two-level loop to reach parent variable.Views10Create Your Own Blade DirectiveIt’s very easy - just add your own method in app/Providers/AppServiceProvider.php . For example, if you want to have this for replace <br tags with new linesViews11Blade Directives: Includeif, Includewhen, IncludefirstIf you are not sure whether your Blade partial file actually would exist, you may use these condition commandsViews12Use Laravel Blade X Variable Binding to Save Even More SpacePractical Laravel guidance for use laravel blade x variable binding to save even more space, with examples and copyable PHP code.Views13Blade Components PropsPractical Laravel guidance for blade components props, with examples and copyable PHP code.Views14Blade Autocomplete TypehintPractical Laravel guidance for blade autocomplete typehint, with examples and copyable PHP code.Views15Component Syntax TipDid you know that if you pass colon (:) before the component parameter, you can directly pass variables without print statement {{ }} ?Views16Automatically Highlight Nav LinksAutomatically highlight nav links when exact URL matches, or pass a path or route name pattern.Views17Cleanup LoopsDid you know the Blade @each directive can help cleanup loops in your templates?Views18Simple Way to Tidy Up Your Blade ViewsA simple way to tidy up your Blade views! Use the forelse loop , instead of a foreach loop nested in an if statementViews19Checked Blade DirectiveIn Laravel 9, you'll be able to use the cool new "checked" Blade directive. This is going to be a nice addition that we can use to clean up our Blade views…Views20Selected Blade DirectiveIn Laravel 9, you'll be able to use the cool new "selected" Blade directive for HTML select elements.Views