From bd22b914cb29ebedcab5458733b931c417aa24c1 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Fri, 3 Dec 2021 18:31:27 +0100 Subject: [PATCH] Weekly thread #48 --- content/authors/JuanDMeGon.md | 5 +++++ content/authors/marcelpociot.md | 5 +++++ content/authors/sebdedeyne.md | 5 +++++ content/threads/weekly-thread-2021-48.md | 11 +++++++++++ ...asone-relation-returning-the-latest-record.md | 15 +++++++++++++++ ...s-in-factories-to-execute-additional-logic.md | 11 +++++++++++ ...ter-the-response-has-been-sent-to-the-user.md | 13 +++++++++++++ ...ation-is-instead-of-comparing-ids-manually.md | 13 +++++++++++++ ...s-parameters-to-laravel-middleware-classes.md | 13 +++++++++++++ ...n-custom-collections-from-eloquent-queries.md | 16 ++++++++++++++++ ...e-keys-and-their-value-types-in-php-arrays.md | 14 ++++++++++++++ ...e-giveconfig-when-using-contextual-binding.md | 13 +++++++++++++ ...or-to-merge-arrays-just-like-in-javascript.md | 13 +++++++++++++ ...-can-validate-user-input-inside-middleware.md | 13 +++++++++++++ 14 files changed, 160 insertions(+) create mode 100644 content/authors/JuanDMeGon.md create mode 100644 content/authors/marcelpociot.md create mode 100644 content/authors/sebdedeyne.md create mode 100644 content/threads/weekly-thread-2021-48.md create mode 100644 content/tips/the-latestofmany-method-lets-you-use-a-hasmany-relation-as-a-hasone-relation-returning-the-latest-record.md create mode 100644 content/tips/use-the-aftermaking-or-aftercreating-hooks-in-factories-to-execute-additional-logic.md create mode 100644 content/tips/use-the-app-gtterminating-hook-to-perform-tasks-after-the-response-has-been-sent-to-the-user.md create mode 100644 content/tips/use-wherebelongsto-and-relation-is-instead-of-comparing-ids-manually.md create mode 100644 content/tips/you-can-pass-parameters-to-laravel-middleware-classes.md create mode 100644 content/tips/you-can-return-custom-collections-from-eloquent-queries.md create mode 100644 content/tips/you-can-use-array-annotations-to-specify-the-available-keys-and-their-value-types-in-php-arrays.md create mode 100644 content/tips/you-can-use-giveconfig-when-using-contextual-binding.md create mode 100644 content/tips/you-can-use-the-spread-operator-to-merge-arrays-just-like-in-javascript.md create mode 100644 content/tips/you-can-validate-user-input-inside-middleware.md diff --git a/content/authors/JuanDMeGon.md b/content/authors/JuanDMeGon.md new file mode 100644 index 0000000..eb0e0a6 --- /dev/null +++ b/content/authors/JuanDMeGon.md @@ -0,0 +1,5 @@ +--- +username: JuanDMeGon +name: 'Juan David Meza' +avatar: 'https://pbs.twimg.com/profile_images/1372280622232768513/NaNa6jTD_normal.jpg' +--- diff --git a/content/authors/marcelpociot.md b/content/authors/marcelpociot.md new file mode 100644 index 0000000..bc21839 --- /dev/null +++ b/content/authors/marcelpociot.md @@ -0,0 +1,5 @@ +--- +username: marcelpociot +name: 'Marcel Pociot πŸ§ͺ' +avatar: 'https://pbs.twimg.com/profile_images/1432253549984075778/JNGTD6wb_normal.jpg' +--- diff --git a/content/authors/sebdedeyne.md b/content/authors/sebdedeyne.md new file mode 100644 index 0000000..e726d9b --- /dev/null +++ b/content/authors/sebdedeyne.md @@ -0,0 +1,5 @@ +--- +username: sebdedeyne +name: 'Sebastian De Deyne' +avatar: 'https://pbs.twimg.com/profile_images/598011661178408960/C4xJt3aO_normal.jpg' +--- diff --git a/content/threads/weekly-thread-2021-48.md b/content/threads/weekly-thread-2021-48.md new file mode 100644 index 0000000..a40acad --- /dev/null +++ b/content/threads/weekly-thread-2021-48.md @@ -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 \ No newline at end of file diff --git a/content/tips/the-latestofmany-method-lets-you-use-a-hasmany-relation-as-a-hasone-relation-returning-the-latest-record.md b/content/tips/the-latestofmany-method-lets-you-use-a-hasmany-relation-as-a-hasone-relation-returning-the-latest-record.md new file mode 100644 index 0000000..7760025 --- /dev/null +++ b/content/tips/the-latestofmany-method-lets-you-use-a-hasmany-relation-as-a-hasone-relation-returning-the-latest-record.md @@ -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 \ No newline at end of file diff --git a/content/tips/use-the-aftermaking-or-aftercreating-hooks-in-factories-to-execute-additional-logic.md b/content/tips/use-the-aftermaking-or-aftercreating-hooks-in-factories-to-execute-additional-logic.md new file mode 100644 index 0000000..dc65d1b --- /dev/null +++ b/content/tips/use-the-aftermaking-or-aftercreating-hooks-in-factories-to-execute-additional-logic.md @@ -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. \ No newline at end of file diff --git a/content/tips/use-the-app-gtterminating-hook-to-perform-tasks-after-the-response-has-been-sent-to-the-user.md b/content/tips/use-the-app-gtterminating-hook-to-perform-tasks-after-the-response-has-been-sent-to-the-user.md new file mode 100644 index 0000000..0c798ad --- /dev/null +++ b/content/tips/use-the-app-gtterminating-hook-to-perform-tasks-after-the-response-has-been-sent-to-the-user.md @@ -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" πŸš€ \ No newline at end of file diff --git a/content/tips/use-wherebelongsto-and-relation-is-instead-of-comparing-ids-manually.md b/content/tips/use-wherebelongsto-and-relation-is-instead-of-comparing-ids-manually.md new file mode 100644 index 0000000..a2862de --- /dev/null +++ b/content/tips/use-wherebelongsto-and-relation-is-instead-of-comparing-ids-manually.md @@ -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. diff --git a/content/tips/you-can-pass-parameters-to-laravel-middleware-classes.md b/content/tips/you-can-pass-parameters-to-laravel-middleware-classes.md new file mode 100644 index 0000000..c04ccae --- /dev/null +++ b/content/tips/you-can-pass-parameters-to-laravel-middleware-classes.md @@ -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. \ No newline at end of file diff --git a/content/tips/you-can-return-custom-collections-from-eloquent-queries.md b/content/tips/you-can-return-custom-collections-from-eloquent-queries.md new file mode 100644 index 0000000..c1ec253 --- /dev/null +++ b/content/tips/you-can-return-custom-collections-from-eloquent-queries.md @@ -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 \ No newline at end of file diff --git a/content/tips/you-can-use-array-annotations-to-specify-the-available-keys-and-their-value-types-in-php-arrays.md b/content/tips/you-can-use-array-annotations-to-specify-the-available-keys-and-their-value-types-in-php-arrays.md new file mode 100644 index 0000000..d1c0d26 --- /dev/null +++ b/content/tips/you-can-use-array-annotations-to-specify-the-available-keys-and-their-value-types-in-php-arrays.md @@ -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 \ No newline at end of file diff --git a/content/tips/you-can-use-giveconfig-when-using-contextual-binding.md b/content/tips/you-can-use-giveconfig-when-using-contextual-binding.md new file mode 100644 index 0000000..5203001 --- /dev/null +++ b/content/tips/you-can-use-giveconfig-when-using-contextual-binding.md @@ -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 \ No newline at end of file diff --git a/content/tips/you-can-use-the-spread-operator-to-merge-arrays-just-like-in-javascript.md b/content/tips/you-can-use-the-spread-operator-to-merge-arrays-just-like-in-javascript.md new file mode 100644 index 0000000..a49bfd5 --- /dev/null +++ b/content/tips/you-can-use-the-spread-operator-to-merge-arrays-just-like-in-javascript.md @@ -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. \ No newline at end of file diff --git a/content/tips/you-can-validate-user-input-inside-middleware.md b/content/tips/you-can-validate-user-input-inside-middleware.md new file mode 100644 index 0000000..ed586af --- /dev/null +++ b/content/tips/you-can-validate-user-input-inside-middleware.md @@ -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: \ No newline at end of file