1 of 28

Laravel 5.8

By Max Snow

maxsnow.me

github.com/specialtactics

2 of 28

Native support for Carbon 2

  • Carbon 2 has a HUGE amount of new features (and changes)
  • Including microsecond precision
  • Previously it was impossible (or very hard) to use it with Laravel, because Laravel requires carbon 1 explicitly
  • Starting with Laravel 5.8, we’ll be able to choose which version of Carbon to use!
  • Migrating to Carbon 2 is a big deal in itself (due to mange changes), but it is well worthwhile, if you have the freedom to address technical debt

3 of 28

Automatic Policy Resolution is Coming to Laravel 5.8

  • Assuming you put your models and policies in conventional locations

4 of 28

Blade Template File Path

  • Compiled Blade templates, will have a comment hint of the template path
  • Provides the opportunity for Blade debugging in PhpStorm through mappings to the blade template

5 of 28

dotenv 3.0

  • More flexibility with which parts of the environment we want to read from
  • First-class support for multiline variables (think keys)
  • No more trimming of values
  • Accept list of paths to use in searching for a .env file
  • Stronger validation of variable names to avoid silent failures of obscure errors

6 of 28

Misc

  • PHPUnit 8 support
  • Event::fire() removed (previously deprecated)
  • Markdown file directory has been changed
  • Additions (ie. fixes) for some irregular plurals (affects Eloquent)
  • Cache locking safety improvements (relevant only if you use cache locking)
  • Deferred Service Providers are now implemented by implementing an interface, rather than setting the $defer property
  • Nexmo / Slack Notification Channels are an independent package now
  • For full list, check https://laravel.com/docs/master/upgrade

7 of 28

Some more interesting changes...

  • Queueing
  • Deprecation of string and array helpers

8 of 28

So you think that a minor package update should have no breaking changes huh?

9 of 28

Taylor Otwell, 2019, trollerised

10 of 28

Caching

Various aspects changed to confirm to PSR-16

11 of 28

Changes in how TTL is specified

  • Previously (version <= 5.7), cache TTL was specified in minutes
  • From version >= 5.8, cache TTL will be specified in seconds
  • So if you are using integers to feed your cache TTL, you will need to make some changes...

// Laravel 5.7 - Store item for 30 minutes...�Cache::put('foo', 'bar', 30);�// Laravel 5.8 - Store item for 30 seconds...�Cache::put('foo', 'bar', 30);

12 of 28

How to update

  • Laravel Shift https://laravelshift.com/
  • Better yet, take this as an opportunity to re-write how you do your TTLs…
  • … by using DateTimeInterface !
  • Carbon example ;

// Laravel 5.7 / 5.8 - Store item for 30 seconds...�Cache::put('foo', 'bar', now()->addSeconds(30));

13 of 28

Other changes

  • Without specifying TTL (defaults to null), items are stored in cache forever
  • Using a TTL of zero or lower, will result in cache invalidation for that key
  • KeyWritten event updates as a result
  • Check out PSR-16 https://www.php-fig.org/psr/psr-16/

14 of 28

Str & Arr helpers

Deprecated & will be removed in 5.9

15 of 28

All global string & array helpers deprecated

  • Array helpers generally begin with array_*
  • For string helpers, it’s a bit more tricky
    • Rule of thumb is, if it’s a global namespace function, then it’s likely affected
  • Check this PR for a full list -
  • Reasoning for this is to follow best practices
    • snake_case global functions are for the PHP STL
    • All of our code should be namespaced and used correctly
  • So all the functionality isn’t leaving laravel - just the global namespace wrappers

16 of 28

New syntax

  • Laravel Shift - https://laravelshift.com/
  • If you prefer to keep using them, pull in https://github.com/laravel/helpers
  • Update your own Helpers to stick to conventions
    • Ie. Prefer to use \App\Support\Helper::function() than global namespaced helpers
  • Use Illuminate\Support\Arr;

17 of 28

18 of 28

Conclusion

An interesting update, which has more features than most, more changes than most - and thus requiring a bit more effort, than most.

So what’s up with Laravel 6?

19 of 28

Bonus

20 of 28

Scheduler...

  • Once upon a time, I wanted to schedule some tasks…..

21 of 28

Scheduler...

  • Then I thought, let’s try all these cool new flags

22 of 28

Scheduler...

  • Then I thought, let’s try all these cool new flags
  • And it broke everything

23 of 28

Scheduler...

  • As it turns out, the “run this task in the background” made it not work
  • I thought I’d look into what it does, so I consulted the manual
  • And it contained no technical info
  • Nobody on slack answered (haven’t tried IRC yet, gotta catch TO)
  • Tried to look into source code, but it’s a bit obfuscated
  • I would think it it just does an & in the CLI so…
  • Not really clear why it would break

24 of 28

Bonus #2

25 of 28

Rounding vs Truncating in PHP

  • Let’s say I want to round to some number of decimals
  • But I don’t want to round it up or down - instead I want to truncate
  • Interesting, there is no way to do it
  • Ie; truncate decimals from 123.459 OR 123.452 to 2 decimal places should return the SAME result - ie;
    • 123.459 = 123.45
    • 123.452 = 123.45
  • Every inbuilt PHP function does a rounding operation, so you will always get a ceil() or floor()

26 of 28

27 of 28

Solution? Wrote my own function to do it

  • Upon searching, there was no way to do it in the language
  • Some weird implementations on stack
  • Wrote & unit tested a function to do it
  • Submitted it as a PR to the math-php package
  • https://github.com/markrogoyski/math-php/pull/287

28 of 28