Top 10 Blade Directives Useful That you Should know

Top 10 Blade Directives Useful That you Should know

In this article I want to share ten Blade directives with you that are very useful and could be used to make your life as a developer easier.

Out of the box Laravel comes with Blade, a powerful templating engine that makes writing your views in Laravel very easy. Most Laravel developers know a lot about the framework, but their Blade knowledge is lacking sometimes. In this article I want to share ten Blade directives with you that are very useful and could be used to make your life as a developer easier.

@json

Instead of manually callingjson_encode($array) in your views you may use the json directive:

@json($array)

If you are using Vue.js for your front end the json directive comes in very handy when seeding Vue-components from your Blade template.

<blog-overview-component :blogs='@json($blogs)'>
</blog-overview-component> 
@isset

A common suboptimal way to check if a variable is set in Blade is by using the if directive.

@if(isset($blog))
   // $blog is defined and is not null
@endif

This could be done via a shortcut, which looks much cleaner and is more readable.

@isset($blog)
   // $blog is defined and is not null
@endisset

The same thing could be done with the empty directive_._

@if (empty($blogs))
    // $blogs is empty
@endif

// Could also be written as:

@empty($blogs)
    // $blogs is empty
@endempty
@guest and @auth

Blade contains authentication directives that could be used to determine if the current user is authenticated or not.

@guest
    // The user is not authenticated...
@endguest

@auth
    // The user is authenticated...
@endauth
@forelse

The forelse directive is a special kind of loop. It is a foreach directive combined with the empty directive. Take a look at the following example:

@if ($blogs->count())
  @foreach ($blogs as $blog)
    <li>{{ $blog->title }}</li>
  @endforeach
@else
  <p>There are no blogs.</p>
@endif

The example above could be refactored into a much cleaner way to solve this problem. This could be done by using the forelse directive.

@forelse ($blogs as $blog)
    <li>{{ $blog->title }}</li>
@empty
    <p>There are no blogs.</p>
@endforelse

Note the difference in readability between both examples.

@continue and @break

Blade has the continue and break directives that make it possible to continue and break inside a loop.

@foreach ($blogs as $blog)
    @if (!$blog->is_validated)
        @continue
    @endif
		
    <li>{{ $blog->title }}</li>
		
    @if ($blog->is_last)
        @break
    @endif
@endforeach

We can optimize the example above by including the condition in the continue and break directive, so that it could be refactored into one line of code:

@foreach ($blogs as $blog)
    @continue(!$blog->is_validated)
		
    <li>{{ $blog->title }}</li>
		
    @break($blog->is_last)
@endforeach

This looks much cleaner, right?

@each

The each directive combines a loop and includes of views in one directive.

@each('blog.item', $blogs, 'blog', 'blog.no-items')

The first argument is the view partial for rendering each element in the array or collection. The second argument is the array or collection that you want to loop through. The third argument is the name of the current element. The last argument, which is optional, is the view partial that will be rendered if the array or collection is empty.

@includeWhen

The include directive allows you to include a Blade view from within another view. A lot of times a check is added to determine if the view should be included.

@if($blog->likes->count())
    @include('view.name', ['blog' => $blog])
@endif

By using the includeWhen directive this could be done in one line of code:

@includeWhen($blog->likes->count(), 'view.name', ['blog' => $blog])
@stack and @push

The stack directive allows you to render content somewhere else in another view or layout. By using the push directive you can add content to a stack. This is often used to render Javascript and CSS includes.

For example, on the blog detail view we could push a Javscript file that needs to be included. We can do this by pushing the HTML to the scripts stack.

@push('scripts')
    <script src="/blog-detail.js"></script>
@endpush

In the __ tag of the layout we could render the complete stack contents by using the stack directive and passing the name of the stack, scripts in this case.

<head>
    <!-- Your head content -->

    @stack('scripts')
</head>
That’s it!

Now that you have read this article I hope that you gained some new insights on Blade directives that you could use. If this article made you start refactoring your views; happy refactoring!

Make sure to check out my other posts aswell, a lot of my content is about Laravel. Please feel free to leave a comment if you have any feedback, questions or want me to write about another Laravel related topic.

Thank you for reading!

Clear Cache in Laravel 6.8 App using Artisan Command Interface (CLI)

Clear Cache in Laravel 6.8 App using Artisan Command Interface (CLI)

In Laravel 6 tutorial, we learn how to use PHP artisan command interface (CLI) to clear the cache from Laravel 6.8 application. How To Clear Cache in Laravel 6.8 Application using Artisan Command Line Interface (CLI)? How to clear route cache using php artisan command? How to easily clear cache in Laravel application? How to clear config cache in PHP Laravel via artisan command? How to clear Laravel view cache? How to Reoptimized class in Laravel via artisan CLI?

Today in this tutorial, we are going to learn how to clear route cache, laravel application cache, config cache, view cache and reoptimized class in a Laravel 6.8 application using artisan command-line interface.

I’m pretty sure many of you may have found yourself gotten into the situation where you do not see changes in the view after making the changes in the app.

Laravel application serves the cached data so caching problem occurs due to the robust cache mechanism of Laravel.

But, if you are still facing this issue, then you do not have to worry further. Let me do the honour of introducing you some of the best artisan commands to remove the cache from your Laravel app via PHP artisan command line interface.

Artisan is the command-line interface included with Laravel. It provides a number of helpful commands that can assist you while you build your application.

Table of Contents

  • Clear Route Cache in Laravel
  • Clear Laravel Application Cache
  • Clear Config Cache via PHP Artisan
  • Clear Laravel View Cache
  • Reoptimized Class
Clear Route Cache in Laravel

Laravel caching system also takes routes in consideration, to remove route cache in Laravel use the given below command:

php artisan route:cache
Clear Application Cache in Laravel

Run the following command to clear application cache:

php artisan cache:clear
Clear Config Cache in Laravel

Run the following command to clear config cache:

php artisan config:cache
Clear View Cache in Laravel

Run the following command to clean your view cache:

php artisan view:clear
Reoptimize Class

Run the below command to reoptimize the class loader:

php artisan optimize

Conclusion

We have completed this Laravel 6 tutorial, In this tutorial we learned how to use php artisan command to clear the cache from your Laravel application. We have answered the following questions in this article.

  • How to clear route cache using php artisan command?
  • How to easily clear cache in Laravel application?
  • How to clear config cache in PHP Laravel via artisan command?
  • How to clear Laravel view cache?
  • How to Reoptimized class in Laravel via artisan CLI?

Now, it’s your time to let me know what do you think about this laravel 6 article. Go forth and try these super awesome artisan commands and let me know how these commands are helping you.

Learn PHP Programming - PHP Tutorial for for Absolute Beginner

Learn PHP Programming - PHP Tutorial for for Absolute Beginner

Learn PHP Programming - PHP Tutorial for for Absolute Beginner: Learn PHP Programming For Web Development The Fast And Easy Way! The PHP Programming Language is one of the most POPULAR Web Development Programming Languages for a reason. In this course I'll show you why!

Learn PHP Programming For Web Development The Fast And Easy Way!

The PHP Programming Language is one of the most POPULAR Web Development Programming Languages for a reason. In this course I'll show you why!

You don't need any prior knowledge of coding or web development to take this course. We'll download all the tools you'll need for free, and I'll walk you through setting them up. Then we'll dive right in and start learning the PHP language.

This is the course for you if you're interested in becoming a web developer and maybe don't know where to start. It's also great if you're already a front end developer, but want to add some serious skills to your tool belt. Since PHP powers Wordpress, this course is also great if you'd like to really customize your Wordpress sites (we don't discuss wordpress in this course, but if you understand PHP, you can understand what's going on behind the scenes in Wordpress just by diving into the code and checking it out on your own).

What you’ll learn

  • How To Download and Use WAMP Server
  • How To Run PHP Locally on your Computer
  • Basic PHP Programming Concepts Like Variables, Data Types
  • Math Operators, Assignment Operators, Comparison Operators
  • How To Do If/Else Statements and Why They're Important
  • How To Loop Using While Loops, For Loops, and Foreach Loops
  • Understanding Indexed Arrays and Why They Are So Great
  • Understanding Associative Arrays (called Hashes or Dictionaries in other Languages)
  • All About Functions - Mini Programs Inside Your Program
  • We'll Build a FizzBuzz app!
  • PHP For Web Development
  • Including PHP on a web page
  • Requiring Things on a Web Page With PHP
  • Using Web Forms With PHP
  • Processing Form Data With PHP
  • And More!

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading about PHP

Build a Basic CRUD App with PHP and MySQL

Build a CRUD Operation using PHP & MongoBD

Laravel 5.8 Tutorial from Scratch for Beginners

Token Authentication in PHP

PHP Interview Questions - Top 50 Questions for PHP Developers

PHP Tutorial for Beginners

Get Weather Data with Laravel Weather

Get Weather Data with Laravel Weather

Get Weather Data with Laravel Weather. Laravel Weather is a good package which we can use to get weather data. It's a wrapper around Open Weather Map API (Current weather). A wrapper around Open Weather Map API (Current weather)

🌤️ A wrapper around Open Weather Map API (Current weather)

Installation

You can install the package via composer:

source-shell
composer require gnahotelsolutions/laravel-weather
Usage
text-html-php
$weather = new Weather();

// Checking weather by city name
$currentWeatherInGirona = $weather->get('girona,es');

// You can use the city id, this will get you unambiguous results
$currentWeatherInGirona = $weather->find('3121456');

Units

By default the package uses metric for Celsius temperature results, this can be modified using the configuration file or on the fly:

text-html-php
$weather = new Weather();

$currentWeatherInGirona = $weather->inUnits('imperial')->get('girona,es');

Language

By default the package uses es for the description translation, this can be modified using the configuration file or on the fly:

text-html-php
$weather = new Weather();

$currentWeatherInGirona = $weather->inLanguage('en')->get('girona');

Guzzle Client Instance

If you need to use another instance of Guzzle, to modify headers for example:

text-html-php
$weather = new Weather();

$guzzle = $this->getSpecialGuzzleClient();

$currentWeatherInGirona = $weather->using($guzzle)->get('girona');

Testing

source-shell
composer test