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 / GenerateTests.php
Last active November 13, 2024 01:53
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
@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 / 1 Types.md
Last active November 14, 2024 11:14
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 / 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 / database-notification-broadcasting.md
Created February 11, 2024 13:27
Optimal method for swiftly broadcasting database notifications in Laravel

Put this into event service providor's boot method

public function boot(): void
{
    // Observe the DatabaseNotification model for created events
    DatabaseNotification::observe(new class {
        
        // Listen to the created event 
        public function created(DatabaseNotification $notification): void
@MrPunyapal
MrPunyapal / CastingEnumFromArrayColumnKey.php
Created January 30, 2024 16:05
Laravel Eloquent Model Tip for Enum Handling with $casts and Array Columns 🚀
<?php
use App\Enums\PostStatus;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Attributes\Attribute;
class Post extends Model
{
// We can easily cast the 'status' column to an enum instance
@MrPunyapal
MrPunyapal / HasLabel.md
Last active January 18, 2024 16:00
Enhance Laravel Enums with Human-Friendly Labels using HasLabel Trait

HasLabel.php Trait

namespace App\Concerns\Enums;
 
trait HasLabel
{
    public function label(): string
    {
        return str($this->name)
@MrPunyapal
MrPunyapal / RefactoredTest.php
Last active January 17, 2024 16:04
test with seeders
<?php
use App\Models\Team;
use App\Models\User;
use Database\Seeders\RoleAndPermissionSeeder;
use Database\Seeders\TeamSeeder;
use Database\Seeders\UserSeeder;
use function Pest\Laravel\actingAs;
use function Pest\Laravel\get;
@MrPunyapal
MrPunyapal / LivewireTeleportGlitch.md
Last active January 17, 2024 16:04
Solution to pesky @teleport glitch during compilation!

Teleport diractive issue

If you encounter caching issues with your @teleport Livewire/Blade directive after running php artisan view:cache, you may notice problems appearing instead of the expected compilation.

image

To resolve this issue, manually add compilation to the Blade directive.

class AppServiceProvider extends ServiceProvider
@MrPunyapal
MrPunyapal / ActivityLogEventSubscriber.php
Last active December 4, 2023 07:57
Laravel Activity Logging with Events and Subscribers
<?php
namespace App\Listeners;
use Illuminate\Events\Dispatcher;
use Illuminate\Foundation\Http\Events\RequestHandled;
use Illuminate\Console\Events\CommandFinished;
use Illuminate\Http\Client\Events\ResponseReceived;
use Illuminate\Support\Facades\Log;