Martin Joo Profile picture
Jan 27 5 tweets 2 min read
🔥Did you know that you can write custom Collections in #Laravel?

You can define a collection to any of your models. It's a great way to organize your code and it also brings it closer to your domain.

Keep reading🧵
1/4

First, you need to define your own collection class.

You can define some very useful methods that you need often when you work with a collection of a given model.

In this example, I need a weighted average of the prices:
2/4

After that, you need to override the newCollection() method of the model.

It tells Laravel that any time you query more than one model it needs to return a TransactionCollection:
3/4

You can use it as a standard Collection 😎
4/4

Summary:
- It makes your models and controllers leaner
- It's very high level, and readable
- It brings your code closer to Domain-Driven Design

Thank you if you're still here! Please retweet the first tweet to spread the word.

• • •

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

Keep Current with Martin Joo

Martin Joo 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 @mmartin_joo

Jan 19
🔥Every #Laravel Eloquent Recipes You’ll Ever Need

I’ve collected my 35 best performing Eloquent-related tweets and put them together in one huge thread. You’ll also find a downloadable PDF below.

🧵Terra-Thread is coming!
1/37
Attribute Cast

In Laravel 8 you can define an accessor in one method with an Attribute cast:
2/37
whereRelation

This example: give every Holding where the Stock relation's ticker column is equal to AAPL:
Read 38 tweets
Jan 11
💡There’s a concept in Laravel called Pipelines or Pipes.

You can chunk larger tasks into smaller parts in a nice composable way.

🧵Keep reading
1/6

This is how the end result looks like. You can send the content through these pipes. Each of them will modify the content and return the new content:
2/6

Each of those pipes is a simple class with a handle method that is similar to a middleware:
Read 7 tweets
Jan 7
Here are my top 10 #Laravel packages📦

I would immediately DIE without them!

🧵Keep reading
1/11 laravel-query-builder by @spatie_be

With this package, you can build Eloquent queries based on the Request. It can handle things like:

- Filtering
- Sorting
- Including relationships
- Sparse fieldsets
2/11 has-many-deep by @staudenmeir

With this package, you can query deep relationships with a single SQL query.

Consider these relationships:
Country → has many → User → has many → Post → has many → Comment
Read 12 tweets
Jan 5
This is the red-green-refactor cycle from the Test-Driven Development world.

It helps you:
- Approach problems from a different perspective
- Avoid bugs
- Build more maintainable applications

🧵Keep reading Image
1/6 The red

First, you write a test. You don’t have any code yet, but you know what you want to write.

So you write a test for the most simple use-case.

Something like that: Image
2/6 The green

(Yes, we are doing the calculator example!)

Now it’s time to make this test pass, or in other words, make it green!

In this phrase you should write the minimum amount of code that makes your test green: Image
Read 7 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

Too expensive? 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 on Twitter!

:(