Skip to content

Instantly share code, notes, and snippets.

View MrPunyapal's full-sized avatar
🐰
Learning slowly

Punyapal Shah MrPunyapal

🐰
Learning slowly
View GitHub Profile
@MrPunyapal
MrPunyapal / 1 Types.md
Last active November 28, 2025 20:31
Types In PHP

Atomic Types (Built-in and Scalar)

// Built-in types
$variable = null;             // null type

// Scalar types
$boolVar = true;              // bool type
$intVar = 42;                 // int type
$floatVar = 3.14;             // float type
@MrPunyapal
MrPunyapal / TimeoutGuard.php
Created June 13, 2025 18:12
PHP Tick-Based Timeout Wrapper (No pcntl Required)
<?php
/**
* TimeoutGuard — A lightweight timeout wrapper using PHP ticks.
*
* This lets you interrupt execution of any callable if it exceeds a time limit.
* Works even on Windows (unlike pcntl).
*
* ⚠️ Requires `declare(ticks=1);`
*/
@MrPunyapal
MrPunyapal / php_constants_cheatsheet.md
Last active October 16, 2025 04:21
PHP Constants Cheat Sheet (8.4 Ready)

🧾 PHP Constants Cheat Sheet (with Examples) — PHP 8.4 Ready

A clean and categorized reference of the most useful constants in PHP.
Includes descriptions and sample outputs for context. Fully valid for PHP 8.3 and safe for PHP 8.4.


🗂 Filesystem & Path Constants

| Constant | Description | Example Output |

@MrPunyapal
MrPunyapal / LaravelWhereLikeMacro.php
Last active October 10, 2025 03:09
Laravel Custom 'whereLike' Macro for Dynamic 'LIKE' Searches including relationships
<?php
namespace App\Providers;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
@MrPunyapal
MrPunyapal / BelongsToTeam.php
Last active March 20, 2025 07:02
Enhance Laravel Access Control with 'BelongsToTeam' Trait: Simplify Team-Based Permissions
<?php
namespace App\Traits\Models;
use App\Models\Team;
use Illuminate\Database\Eloquent\Builder;
trait BelongsToTeam
{
// This method is executed when a new model is being created.
@MrPunyapal
MrPunyapal / GenerateTests.php
Last active February 21, 2025 02:03
Artisan Command for Generating Test Files in Laravel
<?php
Artisan::command('generate-tests', function () {
// Define the base path where class files are located
$files = File::allFiles(base_path('app/Classes'));
foreach ($files as $file) {
// Get the relative path of the current class file and apply string manipulations
$path = str($file->getRelativePathname())
->replace('.php', '') // Remove the .php extension
->replace('/', '\\') // Replace directory slashes with namespace slashes

Tappable Scopes in Laravel

The Problem

  • Laravel's model scopes don't work with Scout (search).
  • Reusing scopes across models means copying code OR using traits (but Scout still breaks).
  • Leads to duplicate query logic.

The Solution: Tappable Scopes

Instead of defining scopes inside models, create scope classes that can be used in both Eloquent & Scout queries!

@MrPunyapal
MrPunyapal / Refactor Enums.md
Last active October 22, 2024 08:26
Refactor enums ffrom snake_case to PascalCase.

Task Completed: Replaced All Enums with Pascal Case from SNAKE_CASE

Steps

  1. Copy both files to the appropriate namespace.
  2. Register the Rector rule:
    ->withRules([
        RenameEnumCasesToPascalCaseRector::class,

])

@MrPunyapal
MrPunyapal / SelectEveryTimeGlobalScopeExample.php
Last active August 29, 2024 14:39
Laravel Eloquent Hack: Automatically Load Relationships and Select Columns 🚀
<?php
// In Laravel, we have the $with property to automatically load a relationship every time.
// However, selecting a specific column by default for every model query doesn't seem possible...
// but here's a hack I found!
class Post extends Model
{
// To automatically load the 'author' relationship on every query
protected $with = ['author'];
@MrPunyapal
MrPunyapal / GenerateUniqueSlug.php
Created November 29, 2023 16:40
Efficient Laravel Slug Generation: Unique, Sleek, and No Looping 🚀
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Blog extends Model
{