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

Add this week's tips

This commit is contained in:
lukinovec 2021-11-12 18:43:31 +01:00
parent 6cce746eb3
commit 32273f8c67
15 changed files with 154 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
username: LarsKlopstra
name: 'Lars Klopstra ⚡'
avatar: 'https://pbs.twimg.com/profile_images/1431208147193057286/dKXhDHT9_normal.jpg'
---

View file

@ -0,0 +1,5 @@
---
username: alexjgarrett
name: 'Alex Garrett-Smith'
avatar: 'https://pbs.twimg.com/profile_images/860958772671500289/63kCnhYX_normal.jpg'
---

View file

@ -0,0 +1,5 @@
---
username: bradyrenting
name: 'Brady Renting'
avatar: 'https://pbs.twimg.com/profile_images/1339877287676788737/Sw4qrlnF_normal.jpg'
---

View file

@ -0,0 +1,5 @@
---
username: taylorotwell
name: 'Taylor Otwell 🪐'
avatar: 'https://pbs.twimg.com/profile_images/1439602105107443714/Vpb3Ra1K_normal.jpg'
---

View file

@ -0,0 +1,5 @@
---
username: tobias_petry
name: 'Tobias Petry'
avatar: 'https://pbs.twimg.com/profile_images/1449987893980766209/iPIfnmTE_normal.jpg'
---

View file

@ -0,0 +1,11 @@
---
slug: weekly-thread-2021-45
title: 'Weekly thread #45 of 2021'
tweet_id: '1459206732329086981'
author_username: archtechx
created_at: 2021-11-12T17:39:54+00:00
links: { }
---
🔥 This week's tips for Laravel developers
As always, huge thanks to everyone making these tweets ❤️

View file

@ -0,0 +1,11 @@
---
title: 'Add a recursive() macro to convert an array including its children to Collections'
tweet_id: '1459206744467410946'
thread_slug: weekly-thread-2021-45
author_username: alexjgarrett
images:
- 'https://pbs.twimg.com/media/FD8ObI6XsAQMldB.jpg'
created_at: 2021-11-12T17:09:33+00:00
slug: add-a-recursive-macro-to-convert-an-array-including-its-children-to-collections
---
A handy @laravelphp Collection macro to convert an array and its children to Collections, using recursion.

View file

@ -0,0 +1,13 @@
---
title: 'Use Common Table Expressions to refine your data in complex Eloquent queries'
tweet_id: '1459206743095791621'
thread_slug: weekly-thread-2021-45
author_username: tobias_petry
images:
- 'https://pbs.twimg.com/media/FDrtE0kWQAYSuwK.jpg'
created_at: 2021-11-12T17:09:32+00:00
slug: use-common-table-expressions-to-refine-your-data-in-complex-eloquent-queries
---
⚡️ Database Tip
Sometimes you have very complex queries involving multiple calculation steps. Instead of nesting queries or doing it in application code you can use Common Table Expressions (CTE) to refine your data in every step and even reuse former steps.

View file

@ -0,0 +1,13 @@
---
title: 'Use the --requests flag when creating models to also create form requests'
tweet_id: '1459206741703356419'
thread_slug: weekly-thread-2021-45
author_username: taylorotwell
images:
- 'https://pbs.twimg.com/media/FDxOtKdWYAUmMwr.jpg'
created_at: 2021-11-12T17:09:32+00:00
slug: use-the-requests-flag-when-creating-models-to-also-create-form-requests
---
This release of Laravel also includes a "--requests" flag when making models via Artisan. This option will create a "Store{Model}Request" and "Update{Model}Request" form request + update the controller to use those requests on their respective methods. 🔥
Thanks @PovilasKorop!

View file

@ -0,0 +1,17 @@
---
title: 'Use the Str::headline() method to convert strings to studly words'
tweet_id: '1459206738872201218'
thread_slug: weekly-thread-2021-45
author_username: bradyrenting
images:
- 'https://pbs.twimg.com/media/FD0rTEbWQAEbf7F.jpg'
created_at: 2021-11-12T17:09:31+00:00
slug: use-the-strheadline-method-to-convert-strings-to-studly-words
---
💡It's refactor time!
Laravel 8.65 introduces the Str::headline() method, this method converts a string into studly words!
A small change like this results in less noise in my code.
Thanks @SteveTheBauman for this implementation! 🙌

View file

@ -0,0 +1,15 @@
---
title: 'Use the withAggregate() method to add related values to Eloquent queries using subselects'
tweet_id: '1459206734686277633'
thread_slug: weekly-thread-2021-45
author_username: pascalbaljet
images:
- 'https://pbs.twimg.com/media/FDrLzISXoAEuCPm.jpg'
created_at: 2021-11-12T17:09:30+00:00
slug: use-the-withaggregate-method-to-add-related-values-to-eloquent-queries-using-subselects
---
💡 A little #Laravel tip from the latest blog post to get the week started!
Under the hood, the withAvg/withCount/withSum and other methods in Eloquent use the 'withAggregate' method. You can use this method to add a subselect based on a relationship 💫
#PHP #webdev #OpenSource

View file

@ -0,0 +1,11 @@
---
title: 'You can add the Prunable trait to soft-delete models from the database, but only remove them permanently after a few days'
tweet_id: '1459206733365084161'
thread_slug: weekly-thread-2021-45
author_username: Philo01
images:
- 'https://pbs.twimg.com/media/FDn5LQNWQAEllPO.jpg'
created_at: 2021-11-12T17:09:30+00:00
slug: you-can-add-the-prunable-trait-to-soft-delete-models-from-the-database-but-only-remove-them-permanently-after-a-few-days
---
Happy Monday 🙌 Let's start the week with a Laravel tip: You can use the Prunable trait to automatically remove models from your database. For example, you can permanently remove soft deleted models after a few days.

View file

@ -0,0 +1,14 @@
---
title: 'You can chain ->can() to Route definitions to specify policy rules'
tweet_id: '1459206740340187141'
thread_slug: weekly-thread-2021-45
author_username: sky_0xs
images:
- 'https://pbs.twimg.com/media/FDx9zXTXIAkeX_-.jpg'
created_at: 2021-11-12T17:09:32+00:00
slug: you-can-chain-can-to-route-definitions-to-specify-policy-rules
---
#Laravel Tip:
If you are running Laravel `v8.70`, you can chain `can()` method directly instead of `middleware('can:..')`
Here is and example:

View file

@ -0,0 +1,13 @@
---
title: 'You can show a link to the previous page by using url()->previous() in a Blade view'
tweet_id: '1459206737529958403'
thread_slug: weekly-thread-2021-45
author_username: LarsKlopstra
images:
- 'https://pbs.twimg.com/media/FD52NI9XsAkjRrc.jpg'
created_at: 2021-11-12T17:09:31+00:00
slug: you-can-show-a-link-to-the-previous-page-by-using-url-previous-in-a-blade-view
---
💡 Check if there's a previous URL when showing error pages instead of linking to static page!
It's incredibly useful if your marketing and app pages share the same error pages! It makes sure your users won't get lost :)

View file

@ -0,0 +1,11 @@
---
title: 'You can use factories for generating ANY fake test data by making them use the Fluent model'
tweet_id: '1459206736183578629'
thread_slug: weekly-thread-2021-45
author_username: SteveTheBauman
images:
- 'https://pbs.twimg.com/media/FDnci06WEAMkBNp.jpg'
created_at: 2021-11-12T17:09:31+00:00
slug: you-can-use-factories-for-generating-any-fake-test-data-by-making-them-use-the-fluent-model
---
I really love using Laravel's Eloquent factories for generating fake test data. Did you know that you can also use them to generate fake API response data by using "Fluent" as the model name? Super cool! 🤯