1
0
Fork 0
mirror of https://github.com/archtechx/laravel-tips.git synced 2025-12-12 05:14:04 +00:00

Merge pull request #16 from archtechx/week-46

Weekly thread #46
This commit is contained in:
Samuel Štancl 2021-11-19 20:03:58 +01:00 committed by GitHub
commit 95735fb3c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 189 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
username: doekenorg
name: 'Doeke Norg'
avatar: 'https://pbs.twimg.com/profile_images/641960403287834624/OLaPNjGt_normal.jpg'
---

View file

@ -0,0 +1,5 @@
---
username: mattkingshott
name: 'Matt Kingshott 🏝'
avatar: 'https://pbs.twimg.com/profile_images/1444979706571415564/toC9_E-T_normal.jpg'
---

View file

@ -0,0 +1,5 @@
---
username: mmartin_joo
name: 'Martin Joo'
avatar: 'https://pbs.twimg.com/profile_images/1456657123639795724/5diaoilq_normal.jpg'
---

View file

@ -0,0 +1,5 @@
---
username: xiCO2k
name: 'Francisco Madeira'
avatar: 'https://pbs.twimg.com/profile_images/1238877427306242055/aspixMYU_normal.jpg'
---

View file

@ -0,0 +1,11 @@
---
slug: weekly-thread-2021-46
title: 'Weekly thread #46 of 2021'
tweet_id: '1461703825039892486'
author_username: archtechx
created_at: 2021-11-19T15:07:06+00:00
links: { }
---
🔥 This week's code tips for Laravel developers
A weekly thread 🧵

View file

@ -0,0 +1,13 @@
---
title: 'Create renderable exceptions'
tweet_id: '1461703839388626946'
thread_slug: weekly-thread-2021-46
author_username: Philo01
images:
- 'https://pbs.twimg.com/media/FEewo1NXMAgaMbb.jpg'
created_at: 2021-11-19T14:32:07+00:00
slug: 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.

View file

@ -0,0 +1,13 @@
---
title: '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'
tweet_id: '1461703833617272840'
thread_slug: weekly-thread-2021-46
author_username: alexjgarrett
images:
- 'https://pbs.twimg.com/media/FEWOM0UXsAQyJ89.jpg'
created_at: 2021-11-19T14:32:05+00:00
slug: including-a-unique-value-like-now-or-strrandom-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!

View file

@ -0,0 +1,11 @@
---
title: 'Use $appends to include properties in the array representation of your model'
tweet_id: '1461703842026770438'
thread_slug: weekly-thread-2021-46
author_username: xiCO2k
images:
- 'https://pbs.twimg.com/media/FEO04rDXsAAQx6i.jpg'
created_at: 2021-11-19T14:32:07+00:00
slug: use-appends-to-include-properties-in-the-array-representation-of-your-model
---
Had to revisit a #Laravel project from 3 years ago, and found out I did append a mutator in the toArray method 😂, there is no need for that, we can use the "$appends" and it does the same job ✅

View file

@ -0,0 +1,13 @@
---
title: 'Use explicit null handling in your DB queries when filtering for rows with empty value in a nullable column'
tweet_id: '1461703840755978242'
thread_slug: weekly-thread-2021-46
author_username: tobias_petry
images:
- 'https://pbs.twimg.com/media/FEEibLTXEAMaNrl.jpg'
created_at: 2021-11-19T14:32:07+00:00
slug: use-explicit-null-handling-in-your-db-queries-when-filtering-for-rows-with-empty-value-in-a-nullable-column
---
⚡️ Database Tip
Comparing anything to a null value is always null too (meaning false). Simple operations like searching for rows with an unequal value will be more complicated. But null safe operators are making those queries dead simple again.

View file

@ -0,0 +1,13 @@
---
title: 'Use the $loop variable in Blade foreach loops to access metadata about the loop'
tweet_id: '1461703837916405761'
thread_slug: weekly-thread-2021-46
author_username: alexjgarrett
images:
- 'https://pbs.twimg.com/media/FEemqXrXIAgeMTB.jpg'
created_at: 2021-11-19T14:32:06+00:00
slug: use-the-loop-variable-in-blade-foreach-loops-to-access-metadata-about-the-loop
---
A handy $loop object is available in Laravel Blade foreach loops. Super useful for controlling output based on the loop position ➰
Source dive for reference: https://github.com/laravel/framework/blob/8.x/src/Illuminate/View/Concerns/ManagesLoops.php

View file

@ -0,0 +1,15 @@
---
title: 'Use the nullOrDelete() method in your relationship column definitions to clear their value when the parent record gets deleted'
tweet_id: '1461703828953198601'
thread_slug: weekly-thread-2021-46
author_username: mmartin_joo
images:
- 'https://pbs.twimg.com/media/FEQ4DcdXsAcePgo.jpg'
created_at: 2021-11-19T14:32:04+00:00
slug: use-the-nullordelete-method-in-your-relationship-column-definitions-to-clear-their-value-when-the-parent-record-gets-deleted
---
🔥#Laravel Migration tip🔥
Do you have a relationship that is nullable?
You can use the nullOnDelete() function. It achieves ON DELETE SET NULL behavior:

View file

@ -0,0 +1,17 @@
---
title: 'Use the *quietly() methods to create models without firing events using your factories'
tweet_id: '1461703827355168772'
thread_slug: weekly-thread-2021-46
author_username: pascalbaljet
images:
- 'https://pbs.twimg.com/media/FEEXi0zXoAAtd7b.jpg'
created_at: 2021-11-19T14:32:04+00:00
slug: 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

View file

@ -0,0 +1,11 @@
---
title: 'Use the something_at convention instead of is_something in your table definitions'
tweet_id: '1461703830240776197'
thread_slug: weekly-thread-2021-46
author_username: alexjgarrett
images:
- 'https://pbs.twimg.com/media/FEAG6YUXEBMceDT.jpg'
created_at: 2021-11-19T14:32:04+00:00
slug: use-the-something-at-convention-instead-of-is-something-in-your-table-definitions
---
Using the 'something_at' convention instead of just a boolean in Laravel models gives you visibility into when a flag was changed like when a product went live.

View file

@ -0,0 +1,13 @@
---
title: 'You can use Schema methods — e.g. table() or create() — outside of migrations'
tweet_id: '1461703835215228932'
thread_slug: weekly-thread-2021-46
author_username: aarondfrancis
images:
- 'https://pbs.twimg.com/media/FEfnHQxWQAwIbVq.jpg'
created_at: 2021-11-19T14:32:06+00:00
slug: you-can-use-schema-methods-eg-table-or-create-outside-of-migrations
---
I don't do it very often, but you can use Laravel's Schema builder anywhere you want, not just in migrations!
If you ever need to make a temp table to help you mass process data more quickly, it's a great solution.

View file

@ -0,0 +1,11 @@
---
title: 'You can use the data_get() method to safely access a nested array value'
tweet_id: '1461703836553265154'
thread_slug: weekly-thread-2021-46
author_username: mattkingshott
images:
- 'https://pbs.twimg.com/media/FEZJDDbWYAE2fiL.jpg'
created_at: 2021-11-19T14:32:06+00:00
slug: 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.

View file

@ -0,0 +1,13 @@
---
title: 'You can use the form="another-form" attribute on buttons to submit a different form'
tweet_id: '1461703831960440837'
thread_slug: weekly-thread-2021-46
author_username: doekenorg
images:
- 'https://pbs.twimg.com/media/FEZQ4UOXEAo2mlv.jpg'
created_at: 2021-11-19T14:32:05+00:00
slug: you-can-use-the-formanother-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.

View file

@ -0,0 +1,15 @@
---
title: 'You can validate values for enum database columns using the new Enum validation rule'
tweet_id: '1461703826033897473'
thread_slug: weekly-thread-2021-46
author_username: sky_0xs
images:
- 'https://pbs.twimg.com/media/FEBiZ_-WQAQv1fY.jpg'
created_at: 2021-11-19T14:32:03+00:00
slug: you-can-validate-values-for-enum-database-columns-using-the-new-enum-validation-rule
---
#Laravel Updates:
In Laravel `8.7` you can now validate `enum` columns!
An Example: