Skip to content

Instantly share code, notes, and snippets.

View jaygaha's full-sized avatar
🎯

Jay Gaha jaygaha

🎯
View GitHub Profile
@jaygaha
jaygaha / laravel-unit-tests-connection.md
Created June 8, 2024 06:34
Override Model connections in Laravel for unit testing

Override Model connections in Laravel for unit testing

If you have specified a different connection that should be used when interacting with a particular model.

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
@jaygaha
jaygaha / soketi-laravel-sail-docker.md
Created May 29, 2024 07:55
Laravel Sail (Docker) + Soketi Setup

Soketi + Laravel Sail (Docker)

Soketi defaults to app-id, app-key, and app-secret after configuring broadcasting environment variables in the.env file. To use our own values, we should define them in docker-compose.yml as follows:

soketi:
    image: "quay.io/soketi/soketi:latest-16-alpine"
    environment:
 SOKETI_DEBUG: "1"
@jaygaha
jaygaha / ClearLaravelCaches.md
Created March 7, 2024 04:07
Using a single command, clear the Laravel caches

Clear Laravel caches using single command

To remove all Laravel caches, use a single artisan command, which saves you a lot of typing.

Instead of performing these individual commands,

$ php artisan cache:clear
$ php artisan config:clear
$ php artisan route:clear
@jaygaha
jaygaha / GetSenderInfoInMessageSendingEventListner.md
Created February 16, 2024 04:59
Get sender & receiver information in Mailable event listner in the Laravel

How to retrieve the name and email address in Laravel listner event to track emails status?

Laravel provides excellent support for email sending. Sometimes you need to record the status of outgoing emails in the system. You must construct two tables: email_logs and email_log_statuses. The 'email_logs' table contains all outgoing emails, while the email_log_statuses table contains all email statuses such as sent, failed, queued, scheduled to send, and so on.

Now, create Event Listeners During the email-sending process, Laravel triggers multiple events. You can set up event listeners to record these events and update the email log accordingly.

php artisan make:listener SendEmailListener --event='Illuminate\Mail\Events\MessageSending'
php artisan make:listener SentEmailListener --event='Illuminate\Mail\Events\MessageSent'
php artisan make:listener FailedEmailListener --event='Illuminate\Mail\Events\MessageFailed'
@jaygaha
jaygaha / laravel-sanctum-auto-logout.md
Created February 6, 2024 02:47
Laravel Sanctum auto logout if authenticated user remains idle for certain time

Laravel Sanctum auto logout if user remains idle for certain time

Ensuring the security of user sessions is critical in web applications, and Laravel Sanctum offers a strong solution for API authentication in Laravel projects. Implementing an automatic logout function when users are idle for an extended length of time is critical for improving security and user privacy. In this post, we'll look at how to integrate idle timeout capabilities with the Laravel API using Sanctum. Let's look at how to set up automated user logout for inactive sessions in a Laravel Sanctum-powered API.

Registering a custom validation with Sanctum

Open AuthServiceProvider.php and add this code accordingly to this file:

use Carbon\Carbon;
@jaygaha
jaygaha / LaravelCustomStubsFile.md
Created January 25, 2024 09:20
Laravel Custom Stubs File

Laravel Custom Stubs File

Artisan make commands build stuff (models, jobs, etc.) from templates. Want to edit those templates? Use php artisan stub:publish to copy them to your app for easy tweaks! This command will publish all the variety of classes available in Laravel inside stubs directory.

Custom Stubs

Using custom stubs, create your own starter template file in Laravel from scratch.

Step 1: Create a Stub File

@jaygaha
jaygaha / laravel-sanctum-csrf-token.md
Created January 12, 2024 02:57
How to make Laravel Sanctum's XSRF-TOKEN cookie available for each environment [Laravel Sanctum]

While I was working on one of my projects using Laravel Sanctum using SPA Authentication. This project has multiple sub-domains for different environments, like DEV, STG, UAT and is hosted in the same domain, like dev.domain.tld, stg.domain.tld. In the .env file, the SESSION_DOMAIN variable should include a leading dot (.) before the subdomain. This ensures that the session cookie is available to all subdomains. For example:

SESSION_DOMAIN=.domain.tld

Basic authentication flow

  1. Access GET sanctum/csrf-cookie to initialize CSRF protection; it will set the session for the request and XSRF-TOKEN as a cookie in the browser. With this session, it will persist.
  2. After issuing the token, the user can now login to the system.
@jaygaha
jaygaha / Laravel-Japanese-Default-Localization.md
Created December 21, 2023 08:39
Laravel Japanese Default Localization #laravel10 #locale #japanese #localization

Publishing The Language Files

By default, the Laravel application skeleton does not include the lang directory. If you would like to customize Laravel's language files or create your own, you should scaffold the lang directory via the lang:publish Artisan command. The lang:publish command will create the lang directory in your application and publish the default set of language files used by Laravel:

php artisan lang:publish

Create ja directory inside lang with en directory. Copy the following files inside it.

@jaygaha
jaygaha / Unable-to-start-php-in-Herd-Laravel.md
Created December 14, 2023 04:16
Solution to fix php not starting in Herd

[Herd] Unable to start PHP

If you are facing this issue while using Herd on your MacBook.

Go to this path /Users/{localUser}/Library/Application Support/Herd/config/fpm/, select installed php version config file like x.x-fpm.conf

Replace user group from staff with admin

group = admin
@jaygaha
jaygaha / MySQL-timestamp-vs-datetime.md
Created November 24, 2023 07:52
mysql, data-type, timestampvsdatetime

MySQL timestamp vs datetime

Both TIMESTAMP and DATETIME store date and time information, but they have some differences in terms of the range they support and the way they handle time zones.

Here are a few things to consider:

  1. Range of Values:
    • TIMESTAMP: The range is '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
    • DATETIME: The range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
  2. Time Zone Handling: