πŸ”₯ Weekly thread #46 of 2021

πŸ”₯ This week's code tips for Laravel developers

A weekly thread 🧡

Create renderable exceptions

11

πŸ”₯ Create renderable exceptions

Create renderable exceptions

Laravel Tip: You can customize how your exceptions are rendered by adding a 'render' method to your exception.

For example, this allows you to return JSON instead of a Blade view when the request expects JSON.

You can use the data_get() method to safely access a nested array value

8

πŸ”₯ You can use the data_get() method to safely access a nested array value

You can use the data_get() method to safely access a nested array value

πŸ’‘#Laravel Tip: Dealing with deeply-nested arrays can result in missing key / value exceptions. Fortunately, Laravel's data_get() helper makes this easy to avoid. It also supports deeply-nested objects.

Including a unique value, like now() or Str::random(), in the Blade template of a Livewire component is a great way to check if that part of the DOM is getting re-rendered

6

πŸ”₯ Including a unique value, like now() or Str::random(), in the Blade template of a Livewire component is a great way to check if that part of the DOM is getting re-rendered

Including a unique value, like now() or Str::random(), in the Blade template of a Livewire component is a great way to check if that part of the DOM is getting re-rendered

A quick and easy way to check if @LaravelLivewire components are re-rendering when you don't want/need them to.

Add now() or now()->timestamp to your component template. The time value will increment if there's a re-render side effect!

You can use the form="another-form" attribute on buttons to submit a different form

5

πŸ”₯ You can use the form="another-form" attribute on buttons to submit a different form

You can use the form="another-form" attribute on buttons to submit a different form

πŸ”₯ I sometimes forget you can use form="form-name" on input types to hook fields inside one form to another. This makes it as if those fields were inside that <form>-tag.

This is super useful if the fields are rendered in the wrong form, but UI wise it makes sense.

Use the *quietly() methods to create models without firing events using your factories

2

πŸ”₯ Use the *quietly() methods to create models without firing events using your factories

Use the *quietly() methods to create models without firing events using your factories

πŸ“ Here's part 2 of the new Database and Eloquent ORM features in @laravelphp 8.x since the release in September 2020.

One of my favorites is the ability in Model Factories to create models without dispatching any events πŸ”₯

Blog post: https://protone.media/en/blog/database-and-eloquent-orm-new-features-and-improvements-since-the-original-laravel-8-release-22

#Laravel #PHP

We're writing a book, you can get it for free here.