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
@sawirricardo
sawirricardo / TransactionEncapsulation.php
Last active November 22, 2023 13:33
It captures responses, handles DB transactions, and allows custom error messages.
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class TransactionEncapsulation
{
@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 / 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 / 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 / LaravelWhereLikeMacro.php
Last active August 5, 2024 08:24
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 / 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 / BelongsToTeam.php
Last active November 28, 2023 11:23
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 / 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 / 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 / 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! 🌟";
}