Update laravel blog posts
This commit is contained in:
parent
0305b71c53
commit
1fecac0979
2 changed files with 32 additions and 14 deletions
|
@ -116,7 +116,7 @@ This query will basically generate a single update to bump the roles.updated_at
|
|||
update roles set roles.updated_at = now() where roles.id in (1, 2, 3)
|
||||
```
|
||||
|
||||
will be executed. (in this example the user has the role 1, 2 and 3 affected)
|
||||
Will be executed. (in this example the user has the role 1, 2 and 3 affected)
|
||||
|
||||
### And the problem?
|
||||
|
||||
|
@ -132,7 +132,9 @@ This limit in MySQL is currently at
|
|||
The way we have handled this situation was simply by not using `$touches`, and manually doing the touches chunk by chunk
|
||||
on the roles to not reach the limit.
|
||||
|
||||
I have chosen to use [listeners](https://laravel.com/docs/8.x/events#registering-events-and-listeners) for that.
|
||||
I have chosen to use [listeners](https://laravel.com/docs/8.x/events#registering-events-and-listeners) for that. The
|
||||
idea behind listeners is really simple: each time a model is _created_, _updated_, _saved_, _deleted_, an event is
|
||||
dispatched, and you can react on it by writing special listener.
|
||||
|
||||
### Define the UserSaved event
|
||||
|
||||
|
@ -148,7 +150,7 @@ and then references it in the model:
|
|||
namespace App\Models;
|
||||
|
||||
/**
|
||||
* @property Collection<Child> $childs
|
||||
* @property Collection<Role> $roles
|
||||
*/
|
||||
class User extends Model
|
||||
{
|
||||
|
@ -180,7 +182,8 @@ class UserListener
|
|||
{
|
||||
public function handleUserSaved(UserSaved $event)
|
||||
{
|
||||
// Manually touches roles
|
||||
// Take roles ids by batch of 1000 and run a single SQL query
|
||||
// to bump updated_at.
|
||||
$event->user->roles()->chunk(1000, function (Collection $role) {
|
||||
Role::whereIn('id', $role->pluck('id'))->update(['updated_at' => Carbon::now()]);
|
||||
});
|
||||
|
@ -199,8 +202,8 @@ I have raised an [issue](https://github.com/laravel/framework/issues/39259) to l
|
|||
After a bit of discussion it has come up that fixing the framework may not be the best thing to do since this use-case
|
||||
is quite rare and the fix is a bit opinionated.
|
||||
|
||||
Therefore, opening a [PR](https://github.com/laravel/docs/pull/7373) in laravel/docs to mention the technical limits
|
||||
of `$touches`
|
||||
Therefore, opening a [pull request](https://github.com/laravel/docs/pull/7373) in laravel/docs to mention the technical
|
||||
limits of `$touches`
|
||||
was the logical follow-up to do. Sadly, the PR was rejected without taking time to think about it.
|
||||
|
||||
I must say I'm a bit disappointed of how the situation has ended, but... *meh*.
|
||||
|
|
|
@ -37,7 +37,7 @@ MAIL_FROM_ADDRESS=no-reply@example.org
|
|||
MAIL_FROM_NAME=Demo App
|
||||
```
|
||||
|
||||
> Note: The initial setup only requires these environment variables because the smtp mailer is already configured in `config/mail.php`.
|
||||
> Note: The initial setup only requires these environment variables because the smtp mailer is already configured by default in `config/mail.php`.
|
||||
|
||||
## 1.2. Creating an email
|
||||
|
||||
|
@ -51,7 +51,7 @@ This command will generate a sample email in `app/Mail/Greetings` and you'll jus
|
|||
|
||||
## 1.3. Sending an email
|
||||
|
||||
Sending an email to a user with Laravel can be either done:
|
||||
Sending an email to a user with Laravel can be either done
|
||||
|
||||
- using `\Illuminate\Notifications\RoutesNotifications::notify`:
|
||||
|
||||
|
@ -77,7 +77,7 @@ Eh! What if we need to 'whitelabelize' our application. :-)
|
|||
|
||||
In our scenario, we have the need to [whitelabelize](https://en.wikipedia.org/wiki/White-label_product) our application:
|
||||
each `User` will belongs to a `Provider` that will have custom SMTP settings. So when sending email to a user we need to
|
||||
configure dynamically the mailer to use the SMTP credentials of `$user->provider`.
|
||||
configure dynamically the mailer to use the SMTP credentials of his provider (`$user->provider`).
|
||||
|
||||
**Can Laravel help us doing so?**
|
||||
|
||||
|
@ -129,13 +129,28 @@ class Provider extends Model
|
|||
}
|
||||
```
|
||||
|
||||
The `Provider` model has many `Users` and has a `mail_configuration` field which is encrypted and that will contain
|
||||
the SMTP credentials.
|
||||
The `Provider` model has many `Users` and has a `mail_configuration` field which is encrypted and that will contain the
|
||||
SMTP credentials.
|
||||
|
||||
The email configuration will be stored as an [encrypted](https://laravel.com/docs/8.x/encryption) JSON configuration. It
|
||||
will look like this:
|
||||
|
||||
```json
|
||||
{
|
||||
"host": "smtp.example.org",
|
||||
"port": 587,
|
||||
"username": "foo",
|
||||
"password": "bar",
|
||||
"encryption": "tls",
|
||||
"from_address": "no-reply@example.org",
|
||||
"from_name": "Example"
|
||||
}
|
||||
```
|
||||
|
||||
## 2.2. Digging down the internals
|
||||
|
||||
Now that our models are ready, we must find a way to use the provider configuration to send the email. Let's dig down
|
||||
in Laravel source code to understand how emails works:
|
||||
Now that our models are ready, we must find a way to use the provider configuration to send the email. Let's dig down in
|
||||
Laravel source code to understand how emails works:
|
||||
|
||||
Remember the two-ways of sending emails?
|
||||
|
||||
|
@ -234,7 +249,7 @@ class AppServiceProvider extends ServiceProvider
|
|||
$transport->setEncryption($parameters['encryption']);
|
||||
|
||||
$mailer = new Mailer('', $app->get('view'), new Swift_Mailer($transport), $app->get('events'));
|
||||
$mailer->alwaysFrom($from_address, $from_name);
|
||||
$mailer->alwaysFrom($parameters['from_address'], $parameters['from_name']);
|
||||
|
||||
return $mailer;
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue