Skip to content

Instantly share code, notes, and snippets.

View jaygaha's full-sized avatar
🎯

Jay Gaha jaygaha

🎯
View GitHub Profile
@jaygaha
jaygaha / lumen-app-key.md
Last active February 3, 2022 07:31
Lumen APP Key generation

Generate APP KEY value for Lumen project

Run following command in console:

sed -i "s|\(APP_KEY=\)\(.*\)|\1$(openssl rand -base64 24)|" .env

This will update .env file with something like APP_KEY=foobar

@jaygaha
jaygaha / Laravel-Dusk.md
Created February 8, 2022 06:24
How to Fix: Facebook\WebDriver\Exception\UnknownErrorException: unknown error: cannot find Chrome binary (Laravel Dusk)

While running following command

php artisan dusk

Gives the chrome binary not found error:

jaygaha@JGAHA:~/workspace/playground/laravel-dusk$ php artisan dusk
@jaygaha
jaygaha / laravel-echo-nuxt3.md
Created May 1, 2023 08:29
Laravel Echo With Nuxt 3

How to use Laravel Echo in Nuxt 3 consuming RESTful API

Laravel as backend in server and Nuxt 3 in frontend

Assuming that Laravel Websockets has been already set up in backend.

Laravel Echo has everything you need for a separate Nuxt 3 front-end application. Let’s install the package with pusher js.

npm i laravel-echo pusher-js
@jaygaha
jaygaha / laravel-cursor-next-pagination.md
Created May 15, 2023 04:58
Cursor pagination will offer better performance if the "order by" columns are indexed. To go next or previous page we can define as.

How to Get 'Next' Cursor for Cursor Paginate In Laravel

use Illuminate\Pagination\Cursor;

...
$nextCursor = $request->has('cursor') ? $request->get('cursor') : null;

$comments = Comment::cursorPaginate(20, ['*'], 'cursor', Cursor::fromEncoded($nextCursor));
@jaygaha
jaygaha / create-mysql-user-grant-permission.md
Created May 19, 2023 08:48
How To Create a New User and Grant Permissions in MySQL8

How To Create a New User and Grant Permissions in MySQL 8

Use the grant permissions command.

If your database name is "newdb" and user is "newuser" the command to grant all privileges on all the tables contained within would be:

Create A New User

mysql> CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
@jaygaha
jaygaha / Quickly-Dumping-Laravel-Queries.md
Created July 26, 2023 04:35
Quickly Dumping Laravel Queries

How to get the raw SQL query from the Laravel Query Builder

Using Laravel Eloquent methods

To get the query from an Eloquent call without actually running it, you can use the toSql() method. This is useful when you only need to see the query and don't want to modify data. However, keep in mind that for complex queries or sub-queries, the toSql() method may not display the entire query.

Example:

$users = User::query()
@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:
@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 / 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 / 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.