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 / CollectionGroupBy.php
Created October 26, 2023 15:33
Did you know you can pass a closure in the GroupBy method of a collection? 🤯
<?php
$posts = Post::filter($filters)->get();
$groupBy = $filters['groupBy'] ?? 'week';
$posts->groupBy(function ($post) use ($groupBy) {
return match ($groupBy) {
'day' => $post->published_at->format('D d M Y'),
// 📅 Group by day, format 'Day Date Month Year'
@MrPunyapal
MrPunyapal / DifferentWaysToAlterTable.php
Created October 31, 2023 14:56
Laravel Database Schema Migration with Multiple Tables
<?php
public function up(): void
{
// Method 1: Adding 'team_id' Foreign Key to Different Tables One by One
Schema::table('users', function (Blueprint $table) {
$table->foreignIdFor(Team::class)->nullable();
});
Schema::table('posts', function (Blueprint $table) {
@MrPunyapal
MrPunyapal / WithAggregate.php
Created October 24, 2023 16:59
🚀 Laravel Tip: Optimize Queries with withAggregate 📊
<?php
// In the Post model
class Post extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
@MrPunyapal
MrPunyapal / ExportReportsLiveiwre.php
Last active November 27, 2023 08:35
Exporting CSV Reports with Renderless Livewire Component
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use Livewire\Attributes\Renderless;
use App\Traits\HasFilteredReports;
class Reports extends Component
{
@MrPunyapal
MrPunyapal / SetTestNow.php
Last active November 27, 2023 16:25
Temporal Tricks with Carbon: A PHP Gist Featuring hasTestNow() and setTestNow()
<?php
// 🎉 Birthday Check Function 🚀
use Carbon\Carbon;
function checkBirthday($birthday)
{
return Carbon::now()->isSameDay($birthday)
? "🎉🥳 Happy Birthday! 🎂 Best wishes for an amazing year ahead! 🚀"
: "🎈 No birthday today. Make it an awesome day anyway! 🌟";
}
@MrPunyapal
MrPunyapal / PostList.php
Created November 20, 2023 15:53
Optimized Load More Laravel livewire
<?php
namespace App\Livewire;
use App\Models\Post;
use Livewire\Component;
class PostList extends Component
{
public $offset = 0;
@MrPunyapal
MrPunyapal / StringMagic.php
Created November 28, 2023 15:48
Exploring PHP 8.3: Alphanumeric String Magic ✨
<?php
// PHP 8.3 introduces exciting new functions for
// incrementing and decrementing alphanumeric strings!
// Let's dive into some examples to see how they work.
// str_increment — Increment an alphanumeric string
echo str_increment("a") . PHP_EOL; // b
@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;
@MrPunyapal
MrPunyapal / ExportCSV.php
Last active December 31, 2023 07:29
PHP Function to Export Products as CSV (without saving it in server)
<?php
function exportCSV()
{
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename=products.csv');
header('Pragma: no-cache');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
@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;