Marian Pop โšก Laravel Magazine Profile picture
Jan 6, 2022 โ€ข 11 tweets โ€ข 5 min read โ€ข Read on X
๐Ÿ”ฅ Epic Laravel Tips Thread ๐Ÿ”ฅ [ Part 1 ] I decided to create weekly threads and share epic @laravelphp tips that will lead to a cleaner and more performant code!

Let's go ๐Ÿงต #epiclaravel ๐Ÿ‘‡๐Ÿป

1/11
๐Ÿ”ฅ Epic Laravel ๐Ÿ”ฅ - Tip No. 1 [ Eloquent Global Query Scopes ] Implementing your own eloquent global query scopes can be a quick and straightforward approach to ensure that all queries for a given model are subject to certain limitations:

2/11
๐Ÿ”ฅ Epic Laravel ๐Ÿ”ฅ - Tip No. 2 [ Conditional Relationships ]
Relationships allow for powerful method chaining and querying. However, for consistency reasons consider creating a separate relationship method that includes the desired chaining or querying:
3/11
๐Ÿ”ฅ Epic Laravel ๐Ÿ”ฅ - Tip No. 3 [ optional() helper method ] The optional function takes any input and allows you to access the object's attributes and methods. If an object is null, properties and methods will return null rather than throwing an error:

4/11
๐Ÿ”ฅ Epic Laravel ๐Ÿ”ฅ - Tip No. 4 [ Order by Relationship ]
Eloquent ORM appears to be a straightforward mapper, yet it has some semi-hidden features and lesser-known uses. Did you know that you can order by relationship? Here's how:

5/11
๐Ÿ”ฅ Epic Laravel ๐Ÿ”ฅ - Tip No. 5 [ Laravel API withoutWrapping(); ]
When utilizing API Resources, the response is automatically wrapped in a 'data' object. Here's how to get rid of it:

6/11
๐Ÿ”ฅ Epic Laravel ๐Ÿ”ฅ - Tip No. 6 [ Disable Lazy Loading ]
Laravel 8.43.0 introduces a new helper method that allows you to disable lazy loading entirely. To do so, in your AppServiceProvider.php, inside the boot() method you refer to this new helper method like this:

7/11
๐Ÿ”ฅ Epic Laravel ๐Ÿ”ฅ - Tip No. 7 [ Pagination Query String ]
If you want to paginate two tables on the same page, you can assign a different query string parameter for each of them:

8/11
๐Ÿ”ฅ Epic Laravel ๐Ÿ”ฅ - Tip No. 8 [ Partial Resource Routes ]
Instead of the entire list of default actions, you can define a subset of actions the controller should handle when defining a resource route:

9/11
๐Ÿ”ฅ Epic Laravel ๐Ÿ”ฅ - Tip No. 9 [ Retrieving a subset of the input data ]
The only and except methods can be used to retrieve a subset of the input data:

10/11
๐Ÿ”ฅ Epic Laravel ๐Ÿ”ฅ - Tip No. 10 [ Testing: spying facades ]
If you want to spy on a facade, use the spy method on that facade. Spies record every interaction between the spy and the code under test, allowing you to make assertions after the code has been run:

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with Marian Pop โšก Laravel Magazine

Marian Pop โšก Laravel Magazine Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @mvpopuk

Jan 22, 2022
๐Ÿ”ฅ #Laravel Eloquent Models - [ ๐Ÿฅท๐Ÿป "hidden" properties ]
We are all familiar with $table, $fillable, $dates and $appends and a few more but did you know there are more undocumented properties that we can make use of in our Laravel models?

Here is the complete list ๐Ÿงต ๐Ÿ‘‡๐Ÿป
1๏ธโƒฃ You can specify the relations to eager load on every query for that model using $with Image
2๏ธโƒฃ You can set how the relationship counts that should be eager loaded on every query for a given model Image
Read 9 tweets
Jan 19, 2022
Laravel makes it simple to create background-processing queued jobs. Your application can reply to web requests with lightning speed and give your clients a better user experience by transferring time-consuming operations to a queue.

Laravel Queues [ Pocket Guide ] ๐Ÿงต ๐Ÿ‘‡๐Ÿป
1๏ธโƒฃ Dispatching and running jobs
You can create a new job running the following artisan command:
2๏ธโƒฃ Queues to database
In order for the queues to use your database, you have to configure the queue connection in queue.php to point to your database. Once that is configured, you have to create two tables in your database: jobs and failed_jobs:
Read 19 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(