Appearance
Blade directives
@php
Do not use it:
- it doesn't provide any value over native
<?php ?>injections - not readable by some tools
diff
- @php $name = $user->email; @endphp
+ <?php $name = $user->email; ?>(as you can see, it's even shorter syntax)
@use
Do not use. Pass as a variable or use Facade instead.
@include
Always explicitly specify data using 2nd parameter:
diff
- @include('home.welcome')
+ @include('home.welcome', ['user' => $user])It will make your code more maintainable and partials more independent.
In a lot of cases you can even use @component syntax instead (for non-components) that works like @include but will not inherit all data available in the parent view:
diff
- @include('home.welcome', ['user' => $user])
+ @component('home.welcome', ['user' => $user])@endcomponentBy doing this, you will make sure on a programmatical level that not any unexpected data will leak from your parent view to a partial.
Directives and whitespace
Do not use whitespace after directive names:
blade
@if($condition)
...
@endif
@foreach()
...
@endforeachWhy: make fewer decisions, focus on writing good code. Laravel's official docs and community tools are not consistent with this whitespace, devs should not spend their mental energy on it.