https://abs.twimg.com/emoji/v2/... draggable="false" alt="🔥" title="Feuer" aria-label="Emoji: Feuer"> Laravel/web development tips. Some of them are not very well known.

1 RT = 1 tip

First 3 are free.

THREAD
1. People underutilize the exception handler.

You can tell the app what response to return when a specific exception is encountered.

This is the simplest example. There& #39;s many more use cases.
2. You can return Mailables as responses from controllers. It will show the rendered version in the browser.

Great for debugging/tweaking the design.
3.1. There& #39;s great value in understanding how object state of Models works. This is mostly relevant in (Feature) tests because they touch many parts of your codebase in one PHP/Laravel App state.

For example, use $model->is($anotherModel) to check if they refer to the same row.
3.2. When a relationship is already loaded and "cached" on the model instance, you& #39;ll have to refresh it.

`$domain->refresh();` to refresh its attributes

`$tenant->load(& #39;primary_domain& #39;);` to update the `primary_domain` relationship on the $tenant instance
4. Laravel Nova lets you show different resources/tools/cards/... based on an if check.

Extremely handy when you have multiple admin panels in a single app.
8. Vue tip. Double indentation of data() sucks.

Use arrow functions instead. https://twitter.com/samuelstancl/status/1285486044561969154">https://twitter.com/samuelsta...
10. If you want to identify teams, workspaces, etc by path, you don& #39;t have to pass the team ID to every route.

In the example below, the value reaches the middleware, where you can do some stuff, and then is REMOVED from the route parameters before being passed to the action.
11. You can use where{X}and{Y}() in Eloquent.

Not recommended in most cases, but interesting.

(image stolen from the internet)
12. When using Nova, I often found myself creating accessors and mutators as the layer between Nova and my models.

Sometimes, you need to transform the user input before storing it.

E.g. prices stored in cents, without tax.

Here& #39;s a better solution: https://twitter.com/vguerrerobosch/status/1306592445895864321">https://twitter.com/vguerrero...
13. If you& #39;re debugging your app and you& #39;d like to examine more things - e.g. the stack trace, executed queries, app context, ...

You can use ddd() instead of dd().

I tend to forget about this one, but it& #39;s super useful.
14. Comparing changes on & #39;saving& #39; using:

$model->getOriginal()

in Eloquent event listeners can be very useful.

One such use case is checking whether an order is locked. If it is, the only write action we permit is unlocking it. Only then can changes be made.
15.1. Eloquent listeners are awesome.

Example: Default value set on creation.
15.2. Example: Deleting associated files on deletion.
15.3. Example: Checking a parent relationship for some things.

Such as, checking if an Order is locked inside an OrderProduct.

OrderProducts cannot be added to/changed in a locked order.
16. Valet for PHP and MySQL/Redis using:
- docker-compose per project
- or DBngin globally

is the absolute best development setup for macOS and Linux.

Fight me.
17. Laravel migrations have a very nice syntax for foreign keys.

I didn& #39;t know about this for so long.

Instead of:
$table->unsignedBigInteger(& #39;user_id& #39;);
$table->foreign(& #39;user_id& #39;)->references(& #39;id& #39;)->on(& #39;users& #39;);

You do:
$table->foreignId(& #39;user_id& #39;)->constrained();
18. You can return "false" from a -ing() Eloquent event listener (creating, updating, saving, deleting) to cancel the action.

This is similar to how I was throwing an exception based on certain things in the example with order locking.
19. Static constructors.

Learned about this idea from @LiamHammett - https://liamhammett.com/static-constructors-in-php-y0zPVbQl

One">https://liamhammett.com/static-co... use case I have for them is running methods in dependencies on ServiceProvider-time #L50">https://github.com/stancl/tenancy/blob/3.x/src/TenancyServiceProvider.php #L50">https://github.com/stancl/te...
20.1. This won& #39;t fit into one tweet but there& #39;s so much cool stuff you can do with Eloquent.

You DON& #39;T have to go raw SQL when you hit a performance issue with Eloquent. There& #39;s so many "secret" techniques.

Example: You can create scopes to add specific selects to your queries.
20.2 You can create dynamic relationships — this relationship is based on a column that& #39;s added using a subquery, in a scope.

And so much more.
20.3. I HIGHLY recommend reading all of @reinink& #39;s free articles.

The last example is from this one:
https://reinink.ca/articles/dynamic-relationships-in-laravel-using-subqueries

20.4.">https://reinink.ca/articles/... This weekend I& #39;ve also been watching his course, where he goes in depth into these things. Greatly recommended as well. https://gumroad.com/a/836072563 ">https://gumroad.com/a/8360725...
21. You can add a tags() method to a job. Anything you return from this method will be displayed in Horizon.

Very useful if you want to tag jobs with things like the user/tenant id.
22. Not too useful but interesting: When I was looking online for some more tips, I found a website by @TightenCo

https://laravel-tricks.com/ 

Users">https://laravel-tricks.com/">... can submit tips and vote on them

Seems like it& #39;s been popular ~5 years ago. Shame that it& #39;s not used anymore, the idea is very nice!
23.1. There& #39;s an interesting difference between global middleware and route-level middleware.

Global middleware is executed BEFORE the route data is parsed. Then the controller constructor is executed. And then the route middleware is executed.
23.2. Most apps won& #39;t need to use this, but this creates an important difference between those two middleware types

Global middleware can be used to affect the application state very early on (e.g. change DB connection based on domain). Before dependencies are injected in ctrlrs
24. This one is less obscure.

The $loop variable.

It greatly complements Tailwind-styled tables.

Striped tables done easily https://abs.twimg.com/emoji/v2/... draggable="false" alt="🎉" title="Partyknaller" aria-label="Emoji: Partyknaller">
25. You can explicitly bind objects to routes. You don& #39;t have to use route model binding only. Custom objects work perfectly well too.

This example is from Tenancy v2 when we had an ActiveRecord-style custom Tenant object.
26. If you& #39;re using Livewire v2... did you know that you can have component methods that listen to *model properties property updates*?

Sounds a bit confusing, so here& #39;s an example: https://twitter.com/LiamHammett/status/1307716271904096262">https://twitter.com/LiamHamme...
aaand it& #39;s bed time, thanks everyone for RTs

I& #39;ll try to continue with as many as possible tomorrow https://abs.twimg.com/emoji/v2/... draggable="false" alt="🔥" title="Feuer" aria-label="Emoji: Feuer">

hope I dream of some laravel tips at night because this is starting to get hard https://abs.twimg.com/emoji/v2/... draggable="false" alt="😅" title="Lächelndes Gesicht mit offenem Mund und Angstschweiß" aria-label="Emoji: Lächelndes Gesicht mit offenem Mund und Angstschweiß">

goodnight! https://abs.twimg.com/emoji/v2/... draggable="false" alt="💤" title="Schlafsymbol" aria-label="Emoji: Schlafsymbol">
27. Throw ValidationExceptions manually, even if you& #39;re not using the Validator.

You& #39;ll get the handling (error bag/JSON response) for free. https://twitter.com/AndreSayej/status/1274399057016958977">https://twitter.com/AndreSaye...
28. Custom collections are extremely underutilized and let you do super cool things like this: https://twitter.com/timacdonald87/status/1253524892055560193">https://twitter.com/timacdona...
You can follow @samuelstancl.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: