Discover and read the best of Twitter Threads about #Laravel

Most recents (24)

Have you ever heard of API versioning?

This is very handy when you need to add an alternative logic for some endpoints but want to keep the current logic for some users.

There are many ways to achieve this. I'm going to show a simple one in this ๐Ÿงต

#Laravel #PHP #PHPC

1/4 Image
1. Create a folder api inside routes

2. Create a file v1.php inside routes/api

3. Create a file v2.php inside routes/api

4. Configure your routes/api.php file like the image

2/4 Image
5. Configure your routes/api/v1.php file like the image

3/4 Image
Read 4 tweets
In recent days I saw several examples of "actions" in #Laravel, but one especially got my attention and I wanted to clarify it - so we had little discussion with @wendell_adriel and I would like to share my opinion about this particular approach ๐Ÿ™‚

๐Ÿงต๐Ÿ‘‡

Before we start, one important preface: I'm big fan of explicitness and testability, so I'm using dependency injection everywhere (where's applicable). Examples in this thread will use it too by default, along with #PHP 8.0+ syntaxes.
What caught my attention was instantiating action inside controller's method ๐Ÿค” That looked suspicious, because I'm used to different nomenclature.

My assumption after looking at the example was that `CreateUserAction` is more like a #CQRS command rather than action.
Read 22 tweets
Praise should be done in public someone said to me once, so I am tweeting instead of emailing you @PovilasKorop
Been trying out a couple of premium courses on @DailyLaravel the last days and this site is lit ๐Ÿ”ฅ

#Laravel #PHP

1/5
My latest discovery is the Laravel Learning Roadmap where you are given โ€œmini examsโ€ where you are tasked with coding something. Your work is automatically checked by GitHub actions upon submitting a PR.

laraveldaily.com/roadmap-learniโ€ฆ

2/5
One thing setting @DailyLaravel apart from other collections of courses is the way @PovilasKorop obv. studies actual usage in the wild and explains not only by theory but also with a practical, comparative look at code implementation of Laravel by different authors.
3/5
Read 5 tweets
As a Laravel Dev who consistently uses Macs, here's a ๐Ÿงต of little tips I have for you when using the command line

#macos #laravel #symfony #phpc
1. If you need to find a command you previously used CTRL + R will let you search for old commands in your ~/.zsh_history file.
2. To move quickly left and right on the CLI, hold the ALT key with the arrow keys and it will force the cursor left and right to the next white space in the command.
Read 8 tweets
As a PHP developer, there are a great number of static tools that you can use. Here are four off the top of my head that I regularly go to:

#php #laravel ๐Ÿงต
@phpstan, this tool is great for finding little oddities in your code you might not even notice yourself. Things like unused variables or impossible scenarios. Larastan extends it for Laravel apps to handle some of the magic involved. github.com/nunomaduro/larโ€ฆ
@phpstan Laravel Pint is the next one, it's PHP CS Fixer under the hood but a little bit nicer in just it's easy to install and set up with no additional dependencies. laravel.com/docs/9.x/pint
Read 6 tweets
There were many discussions if #Laravel's facades implement GoF's Facade Pattern, but I think it does not matter at this point - the team won't change naming convention anyway. Naming is not a problem, I see other issues with facades - a #PHP thread ๐Ÿงต
1) They're basically magic ๐Ÿช„ Some may see it as advantage, but I consider it as drawback. You don't execute exact code you're calling, but your call is proxied to some underlying service. It strictly couples your code with the framework, which handles it.
Facades' API has to be added as comments in PHPDoc (with `@Method`) which is error prone because it's easy to forget to update facade's phpdoc when underlying service (accessor) is changed. But even if autocompletion in IDE works, you just can't simply navigate to method's code.
Read 18 tweets
#VSCode extensions for #PHP / #Laravel developer

1. Auto Rename Tag
2. PHP Intelephense
3. PHP Namespace Resolver
4. PHP Debug
5. PHP CS Fixer for Visual Studio Code (code formatter)
6. PHP Create Class
7. PHP Constructor
8. Path Intellisense

๐ŸงตThread
9. PHP Namespace Resolver
10. Laravel 5 Snippets
11. Laravel Blade Snippets
12. Laravel Blade Spacer
13. Laravel Extra Intellisense
14. Laravel goto view
15. Laravel Snippets
16. Prettier - Code formatter (JS, HTML, GraphQL ยท Markdown ยท YAML formatter)
17. JavaScript (ES6) code snippets
18. IntelliSense for CSS class names in HTML
Read 4 tweets
#Laravel: Create custom command to make custom class. The example shows how we create a DataTransferObject (DTO) Class by command

๐ŸงตThread MakeDTOCommand.php
Above code will generate DTO class for us. You need some changes.

You need to change the stub path (location of your stub file) on getStub() Image
You need to change the DTO Class savings directory (location of your DTOclasses) on getDefaultNamespace() Image
Read 7 tweets
๐ŸŒถ๏ธ #Laravel Tip: When testing with #PHPUnit, include phpunit.xml and phpunit.xml.dist files. PHPUnit first tries to use phpunit.xml; if that file doesn't exist, it tries to use phpunit.xml.dist instead.

PHPUnit only uses one of these files, never both.

๐Ÿงต Thread ๐Ÿงต ๐Ÿ‘‡
1๏ธโƒฃ

๐ŸŒŸ ๐—•๐—ฒ๐˜€๐˜ ๐—ฃ๐—ฟ๐—ฎ๐—ฐ๐˜๐—ถ๐—ฐ๐—ฒ๐˜€ ๐ŸŒŸ

1๏ธโƒฃ Include phpunit.xml.dist file in your Git repo
2๏ธโƒฃ Add phpunit.xml file to your .gitignore file
2๏ธโƒฃ

๐ŸŒŸ ๐—›๐—ผ๐˜„ ๐—ฃ๐—›๐—ฃ๐—จ๐—ป๐—ถ๐˜ ๐˜„๐—ผ๐—ฟ๐—ธ๐˜€ ๐ŸŒŸ

PHPUnit will use phpunit.xml.dist, but if you want to run a modified set of the tests, they can copy your phpunit.xml.dist file into phpunit.xml and then modify phpunit.xml.
Read 4 tweets
0โƒฃClean Code Series: Functions ๐Ÿ“’๐Ÿ’ป

A guide to producing readable, reusable, and refactorable software in #php (#laravel #symfony #magento) #javascript #python, etc.

(References)
- Robert C. Martin's book "Clean Code"
- github .com/jupeter/clean-code-php

Mega Thread (14) ๐Ÿงต๐Ÿ‘‡ Image
1โƒฃ Clean Code Series: Functions๐Ÿ“’๐Ÿง‘โ€๐Ÿ’ป

โ˜‘๏ธ Use default arguments instead of short-circuiting or conditionals Image
2โƒฃ Clean Code Series: Functions๐Ÿ“’๐Ÿง‘โ€๐Ÿ’ป

โ˜‘๏ธ Function arguments (2 or fewer ideally)

๐Ÿ”นZero arguments are the ideal case. One or two arguments are ok, and three should be avoided. Anything more than that should be consolidated. Image
Read 16 tweets
๐ŸŒถ #Laravel Tip: If you don't like magic (like me) & want to dig more into understanding things, this thread is for you!

โœด๏ธ ๐—›๐—ผ๐˜„ ๐—จ๐˜€๐—ฒ๐—ฟ ๐—ฅ๐—ฒ๐—ด๐—ถ๐˜€๐˜๐—ฟ๐—ฎ๐˜๐—ถ๐—ผ๐—ป & ๐—ฉ๐—ฒ๐—ฟ๐—ถ๐—ณ๐˜† ๐—˜๐—บ๐—ฎ๐—ถ๐—น ๐˜„๐—ผ๐—ฟ๐—ธ ๐—ถ๐—ป ๐—Ÿ๐—ฎ๐—ฟ๐—ฎ๐˜ƒ๐—ฒ๐—น (๐—•๐—ฟ๐—ฒ๐—ฒ๐˜‡๐—ฒ)

Let's start ๐Ÿ’ช

๐Ÿงต Thread ๐Ÿงต ๐Ÿ‘‡
2/8 โœจโœจ Prerequisites โœจโœจ

๐Ÿ“ Create a new Laravel app
๐Ÿ“ Install the Breeze package. In my case, I installed the Breeze package with the option of --๐—ถ๐—ป๐—ฒ๐—ฟ๐˜๐—ถ๐—ฎ
3/8 โœจโœจ Register a new User โœจโœจ

๐Ÿ“ Implement the MustVerifyEmail on the User model
๐Ÿ“ Register a new User
๐Ÿ“ The RegisteredUserController::store dispatches the Registered event
๐Ÿ“ Event listener, SendEmailVerificationNotification sends User an email to verify
Read 8 tweets
Just realised that #Laravel is like the #jQuery of PHP. I get it now.

The focus is on productivity and ease of use, rather than mandating SOLID and unit testable principles the entire way.

If you want that stuff then you'll use TypeScript/SPA, instead of jQuery.

1/2
Yes you can do that stuff in jQuery/Laravel, it's just not the primary focus of the docs/community and that's fine.

I personally did the SOLID/DDD approach with Laravel in 2016, for an enterprise client and it was really challenging to change the culture then, but it has matured
So sorry @taylorotwell for the resistance so far, I do get it now

I'm more set on the expectations of people using Laravel for software without expecting SOLID everywhere, and when I hire people I get that it's not their style or approach and that's okay in the right environment
Read 4 tweets
Iโ€™ve recently started doing @laravelphp security audits & pentests, and thought it would be interesting to share the process I've developed for doing them.

#Laravel #Security ๐Ÿงต (Is this a thing now?)
The first thing I do when starting an audit is to run a couple of passive scans/checks on the public page of the app, and open view source (๐Ÿ˜ฑhacker!). These help give an idea of the security awareness level of the dev team, and what to expect in the code.
The main one is @securityheaders (securityheaders.com), but ssllabs.com/ssltest/ and hstspreload.org are also useful, and I always check for a securitytxt.org file.
Plus, if it's an SPA, I'll look things like config, routes (i.e. Ziggy), translations, etc.
Read 17 tweets
0โƒฃClean Code Series: Comparision ๐Ÿ“’๐Ÿ’ป

A guide to producing readable, reusable, and refactorable software in #php (#laravel #symfony) #javascript #python, etc.

- Software engineering principles, from Robert C. Martin's book "Clean Code"

๐Ÿ”ฝ github.com/jupeter/clean-โ€ฆ

Thread ๐Ÿงต๐Ÿ‘‡ Image
1โƒฃClean Code Series: Comparision ๐Ÿ“’๐Ÿ’ป

โ˜‘๏ธ Use identical comparison Image
2โƒฃClean Code Series: Comparision ๐Ÿ“’๐Ÿ’ป

โ˜‘๏ธ Null coalescing operator Image
Read 5 tweets
๐Ÿ”ฅ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:
Read 5 tweets
In general, I think it's good advice, but I guess it comes down to what you consider โ€œcloseโ€.

For our bigger apps that have lots of logic and models we like to work with a domain oriented approach.

๐Ÿงต A few examples and thoughts on this.


#laravel #php
A domain is basically a way of grouping the logic of some parts so that you can work on that small parts without having to deal with all classes in your app.

It's sound heavy but it's actually a pretty lightweight practice. We talk about it in our book: laravel-beyond-crud.com
The "stick close to the default structure" mantra is the result of people who tried to go too far by putting stuff outside of the app directory, requiring service providers, splitting up resources/config directories.

When refactoring to domains, you're only changing a namespace.
Read 8 tweets
๐Ÿ”ฅ #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
๐Ÿ”ฅ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
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
The fundamental problem of #web #development:

Turning trees into tables and back

(Thread 1/6)
It may seem simple as you may have some data in tables (hello, MySQL) and you want them on a web page (hi, <table>). But you start joining data from other tables and you end up displaying a tree in a table. How? You never know how to do it until you try to use that. UX!

(2/6)
But if you have tree-data, tree-database (hello, Mongo), and tree components on website, you still need tables to present the data of similar structure. It's all 2D and are brains are trained to get information presented as combination of 2 axes. Hello, tables!

(3/6)
Read 7 tweets
๐Ÿงต Today I decided to tackle a performance issue in our #MySQL Database.

We use timestamp columns for storing states (is paid, is invoiced, is shipped etc.) and clients would like to be able to filter by these states (see screenshot)

Thread below๐Ÿ‘‡๐Ÿป
Despite having indexes on all 4 of our timestamp columns, this query can become incredibly slow as data grows. Screenshot from first tweet is an example on 1 million records.

In order to optimize this, we can add a indexed boolean state column that concats all the timestamps.
Notice the leading "1", this is simply to ensure no leading zeroes are stripped. Using a string column to combat this would result in worse performance, as querying integers is much faster.

This change means we can now query by the state column instead of the timestamp column.
Read 7 tweets
When adding Octane to an existing @laravelphp app, spotting memory leaks can be quite tricky. ๐Ÿ”Ž

This thread lists some basic techniques that helped me out, hopefully it can be useful to other devs. ๐Ÿงต

#LaravelOctane #laravel #php
@laravelphp During development, make sure to display/log memory usage peaks in all your pages/scripts.

If the peak keeps increasing esponentially at every refresh, there is a memory leak in the current page/script. ๐Ÿ˜
@laravelphp As suggested by the Laravel team, the first thing to do is checking for singleton bindings in all service providers (yours and 3rd party packages').

Singletons shouldn't inject or call instances of Application, Request or config Repository classes. โš ๏ธ
Read 11 tweets
Top video resources/courses to help you become better Laravel Developer

Thread ๐Ÿงต

#Laravel #PHP
Read 10 tweets
Next up we have @calebporzio talking about @LaravelLivewire and @Alpine_JS

Thread ๐Ÿงต
of Livewire. Woah!
Show the small things, instead of big things.

Shoutout to @TransistorFM and @noplanstomerge
Read 26 tweets

Related hashtags

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.00/month or $30.00/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 Become our Patreon

Thank you for your support!