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 #18 from archtechx/week-48

Weekly thread #48
This commit is contained in:
Samuel Štancl 2021-12-03 18:42:13 +01:00 committed by GitHub
commit 83c15468e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 160 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
username: JuanDMeGon
name: 'Juan David Meza'
avatar: 'https://pbs.twimg.com/profile_images/1372280622232768513/NaNa6jTD_normal.jpg'
---

View file

@ -0,0 +1,5 @@
---
username: marcelpociot
name: 'Marcel Pociot 🧪'
avatar: 'https://pbs.twimg.com/profile_images/1432253549984075778/JNGTD6wb_normal.jpg'
---

View file

@ -0,0 +1,5 @@
---
username: sebdedeyne
name: 'Sebastian De Deyne'
avatar: 'https://pbs.twimg.com/profile_images/598011661178408960/C4xJt3aO_normal.jpg'
---

View file

@ -0,0 +1,11 @@
---
slug: weekly-thread-2021-48
title: 'Weekly thread #48 of 2021'
tweet_id: '1466810181308715009'
author_username: archtechx
created_at: 2021-12-03T17:27:54+00:00
links: { }
---
🔥 This week's code tips for Laravel developers
🧵 A weekly thread

View file

@ -0,0 +1,15 @@
---
title: 'The latestOfMany() method lets you use a ''hasMany'' relation as a ''hasOne'' relation, returning the latest record'
tweet_id: '1466810194193575939'
thread_slug: weekly-thread-2021-48
author_username: freekmurze
images:
- 'https://pbs.twimg.com/media/FFT_7UUXsAUxtYF.jpg'
created_at: 2021-12-03T16:42:56+00:00
slug: the-latestofmany-method-lets-you-use-a-hasmany-relation-as-a-hasone-relation-returning-the-latest-record
---
🔥 In an Eloquent model can define a relation that will get the newest (or oldest) item of another relation. Very nice!
📘 Docs: https://laravel.com/docs/8.x/eloquent-relationships#has-one-of-many
#laravel #php

View file

@ -0,0 +1,11 @@
---
title: 'Use the ''afterMaking'' or ''afterCreating'' hooks in factories to execute additional logic'
tweet_id: '1466810192801120259'
thread_slug: weekly-thread-2021-48
author_username: mattkingshott
images:
- 'https://pbs.twimg.com/media/FFXsvEtXsAIrx_I.jpg'
created_at: 2021-12-03T16:42:56+00:00
slug: use-the-aftermaking-or-aftercreating-hooks-in-factories-to-execute-additional-logic
---
💡#Laravel Tip: Database model factories have handy 'afterMaking' and 'afterCreating' callback methods that you can use to perform additional tasks on newly-created objects.

View file

@ -0,0 +1,13 @@
---
title: 'Use the app()->terminating() hook to perform tasks *after* the response has been sent to the user'
tweet_id: '1466810187348463620'
thread_slug: weekly-thread-2021-48
author_username: marcelpociot
images:
- 'https://pbs.twimg.com/media/FFeI02qXIAENzFj.jpg'
created_at: 2021-12-03T16:42:55+00:00
slug: use-the-app-gtterminating-hook-to-perform-tasks-after-the-response-has-been-sent-to-the-user
---
🔥 Use the "terminating" hook on your @laravelphp app instance to perform tasks _after_ the response was already sent to the browser.
That's also what's powering "dispatchAfterResponse" 🚀

View file

@ -0,0 +1,13 @@
---
title: 'Use whereBelongsTo() and relation()->is() instead of comparing ids manually'
tweet_id: '1466810182265032710'
thread_slug: weekly-thread-2021-48
author_username: archtechx
images:
- 'https://pbs.twimg.com/media/FFXqNWlVUAElTew.jpg'
created_at: 2021-12-03T16:42:54+00:00
slug: use-wherebelongsto-and-relation-is-instead-of-comparing-ids-manually
---
🔥 Tip: You don't have to use "related_id"-type columns in database queries or value comparisons.
Laravel understands your relations and lets you write much cleaner code.

View file

@ -0,0 +1,13 @@
---
title: 'You can pass parameters to Laravel middleware classes'
tweet_id: '1466810184773160962'
thread_slug: weekly-thread-2021-48
author_username: marcelpociot
images:
- 'https://pbs.twimg.com/media/FFXH9CSXwAEiMm0.jpg'
created_at: 2021-12-03T16:42:54+00:00
slug: you-can-pass-parameters-to-laravel-middleware-classes
---
🔥 Remember that you can pass parameters to your custom @laravelphp middleware classes
This simple middleware allows me to inject onboarding screens in an Inertia app.

View file

@ -0,0 +1,16 @@
---
title: 'You can return custom collections from Eloquent queries'
tweet_id: '1466810189018185728'
thread_slug: weekly-thread-2021-48
author_username: freekmurze
images:
- 'https://pbs.twimg.com/media/FFRsxnbXsAM2PHo.jpg'
created_at: 2021-12-03T16:42:55+00:00
slug: you-can-return-custom-collections-from-eloquent-queries
---
If you want to have Eloquent return a custom collection class (so you can define methods of your own on it, you can the `newCollection` method on your model.
More in this excellent post by @timacdonald87
https://timacdonald.me/giving-collections-a-voice/
#laravel #php

View file

@ -0,0 +1,14 @@
---
title: 'You can use array{} annotations to specify the available keys (and their value types) in PHP arrays'
tweet_id: '1466810191521869825'
thread_slug: weekly-thread-2021-48
author_username: freekmurze
images:
- 'https://pbs.twimg.com/media/FFrjS3xXwAIdNuy.jpg'
created_at: 2021-12-03T16:42:56+00:00
slug: you-can-use-array-annotations-to-specify-the-available-keys-and-their-value-types-in-php-arrays
---
🔥When you add an `array{}` return docblock, IDEs are able to autocomplete the keys when reaching into that array.
Sweet!
#php

View file

@ -0,0 +1,13 @@
---
title: 'You can use giveConfig() when using contextual binding'
tweet_id: '1466810190271946757'
thread_slug: weekly-thread-2021-48
author_username: sebdedeyne
images:
- 'https://pbs.twimg.com/media/FFdDMbgUcA4YjoD.jpg'
created_at: 2021-12-03T16:42:56+00:00
slug: you-can-use-giveconfig-when-using-contextual-binding
---
Most of the times I've used contextual binding in Laravel, it's been to share config values.
TIL there's a more succinct giveConfig method for that

View file

@ -0,0 +1,13 @@
---
title: 'You can use the spread operator to merge arrays, just like in JavaScript'
tweet_id: '1466810186002096137'
thread_slug: weekly-thread-2021-48
author_username: mmartin_joo
images:
- 'https://pbs.twimg.com/media/FFS1obRWQAgZFTB.png'
created_at: 2021-12-03T16:42:55+00:00
slug: you-can-use-the-spread-operator-to-merge-arrays-just-like-in-javascript
---
A quick reminder about a not-so-often used #PHP feature.
We have the spread operator, and it works just like the one in JavaScript.

View file

@ -0,0 +1,13 @@
---
title: 'You can validate user input inside middleware'
tweet_id: '1466810183431032836'
thread_slug: weekly-thread-2021-48
author_username: JuanDMeGon
images:
- 'https://pbs.twimg.com/media/FFK5Rd-XwAAQ-g5.jpg'
created_at: 2021-12-03T16:42:54+00:00
slug: you-can-validate-user-input-inside-middleware
---
✔ You can validate fields even within a middleware in Laravel.
This would help when you need to constantly validate the same field(s) on your requests: