Laravel 11 Event & Listener Send Email User Registered

Dispatch event when user is registered and send welcome email
// php artisan make:event UserRegistered
// app/Events/UserRegistered.php
namespace App\Events;

use App\Models\User;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class UserRegistered
{
    use Dispatchable, SerializesModels;

    public $user;

    /**
     * Create a new event instance.
     *
     * @param \App\Models\User $user
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }
}

// php artisan make:listener SendWelcomeEmail --event=UserRegistered
// app/Listeners/SendWelcomeEmail.php
namespace App\Listeners;

use App\Events\UserRegistered;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Mail;
use App\Mail\WelcomeEmail;

class SendWelcomeEmail implements ShouldQueue
{
    use InteractsWithQueue;

    /**
     * Handle the event.
     *
     * @param \App\Events\UserRegistered $event
     */
    public function handle(UserRegistered $event)
    {
        // Send welcome email using the $event->user data
        Mail::to($event->user->email)->send(new WelcomeEmail($event->user));
    }
}

// php artisan make:mail WelcomeEmail
// app/Mail/WelcomeEmail.php
namespace App\Mail;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class WelcomeEmail extends Mailable
{
    use Queueable, SerializesModels;

    public $user;

    /**
     * Create a new message instance.
     *
     * @param \App\Models\User $user
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.welcome')->with([
            'userName' => $this->user->name,
        ]);
    }
}
// resources/views/emails/welcome.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>Welcome to BBMCode.com!</title>
</head>
<body>
    <h1>Hello, {{ $userName }}</h1>
    <p>Thank you for registering at our platform.</p>
</body>
</html>

// Dispatch event
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Events\UserRegistered;
use Illuminate\Http\Request;

class RegisterController extends Controller
{
    public function register(Request $request)
    {
        // Validate and create the user
        $user = User::create($request->only('name', 'email', 'password'));

        // Dispatch the event
        event(new UserRegistered($user));

        return redirect()->route('home')->with('success', 'Registration successful.');
    }
}

// register the event and listener
// app/Providers/EventServiceProvider.php
protected $listen = [
    \App\Events\UserRegistered::class => [
        \App\Listeners\SendWelcomeEmail::class,
    ],
];

PHP Sorting an array by multiple fields

sort multiple arrays or an array of associative arrays by different fields using array_multisort.
// Sorting an array by multiple fields
$people = [
    ['name' => 'John', 'age' => 28, 'city' => 'New York'],
    ['name' => 'Alice', 'age' => 24, 'city' => 'Los Angeles'],
    ['name' => 'Bob', 'age' => 32, 'city' => 'Chicago'],
    ['name' => 'Charlie', 'age' => 24, 'city' => 'San Francisco']
];

// Create arrays for the fields you want to sort by
$age = array_column($people, 'age');
$name = array_column($people, 'name');

// Use array_multisort to sort by age (ascending) and then by name (ascending)
array_multisort($age, SORT_ASC, $name, SORT_ASC, $people);

print_r($people);

// output
Array
(
    [0] => Array
        (
            [name] => Alice
            [age] => 24
            [city] => Los Angeles
        )

    [1] => Array
        (
            [name] => Charlie
            [age] => 24
            [city] => San Francisco
        )

    [2] => Array
        (
            [name] => John
            [age] => 28
            [city] => New York
        )

    [3] => Array
        (
            [name] => Bob
            [age] => 32
            [city] => Chicago
        )
)

// Sorting by multiple fields with different sort orders
$people = [
    ['name' => 'John', 'age' => 28, 'city' => 'New York'],
    ['name' => 'Alice', 'age' => 24, 'city' => 'Los Angeles'],
    ['name' => 'Bob', 'age' => 32, 'city' => 'Chicago'],
    ['name' => 'Charlie', 'age' => 24, 'city' => 'San Francisco']
];

// Create arrays for age and name
$age = array_column($people, 'age');
$name = array_column($people, 'name');

// Sort by age descending and by name ascending
array_multisort($age, SORT_DESC, $name, SORT_ASC, $people);

print_r($people);

// output
Array
(
    [0] => Array
        (
            [name] => Bob
            [age] => 32
            [city] => Chicago
        )

    [1] => Array
        (
            [name] => John
            [age] => 28
            [city] => New York
        )

    [2] => Array
        (
            [name] => Alice
            [age] => 24
            [city] => Los Angeles
        )

    [3] => Array
        (
            [name] => Charlie
            [age] => 24
            [city] => San Francisco
        )
)

PHP Convert Vietnamese Without Diacritics

Convert Vietnamese text with diacritics to plain text (without diacritics). Useful for friendly URL
function convertVItoEN($str) {
    $str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", "a", $str);
    $str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", "e", $str);
    $str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", "i", $str);
    $str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", "o", $str);
    $str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", "u", $str);
    $str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", "y", $str);
    $str = preg_replace("/(đ)/", "d", $str);
    $str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", "A", $str);
    $str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", "E", $str);
    $str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", "I", $str);
    $str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", "O", $str);
    $str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", "U", $str);
    $str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", "Y", $str);
    $str = preg_replace("/(Đ)/", "D", $str);
    $str = str_replace(" ", "-", str_replace("&*#39;","",$str));
    return strtolower($str);
    // thật hữu ích -> that-huu-ich
}