Premium
PHP Snippets.

Curated list of production-ready PHP scripts and coding solutions.

PHP

Creating or Retrieving Models with Eloquent `firstOrCreate` and `firstOrNew`

Discover Laravel Eloquent's `firstOrCreate` to find or create a model, and `firstOrNew` to instantiate a model without saving, for flexible data management.

View Snippet →
PHP

Filtering PHP Arrays with a Custom Callback

Learn how to filter elements from a PHP array using `array_filter` and a custom callback function, allowing precise data selection based on your specific criteria and logic.

View Snippet →
PHP

Sorting Multi-Dimensional PHP Arrays by Key

Discover how to sort complex PHP arrays of associative arrays (e.g., list of users) by a specific key's value using `usort` and a custom comparison function for precise ordering.

View Snippet →
PHP

Extracting a Column from a PHP Array of Arrays

Quickly retrieve all values from a specific column across an array of associative arrays in PHP using `array_column`, ideal for lists of records like user data or product information.

View Snippet →
PHP

Managing Eloquent Models with Soft Deletes

Learn how to implement soft deletes in Laravel Eloquent models, allowing you to gracefully "delete" records without permanent data loss, and how to query, restore, or force delete them.

View Snippet →
PHP

Filtering Eloquent Models by Related Model Conditions with `whereHas`

Learn how to filter your main Eloquent models based on the existence or specific conditions of their related models using `whereHas` and `orWhereHas` methods in Laravel.

View Snippet →
PHP

Implementing Custom Eloquent Casts for Value Objects

Extend Laravel Eloquent's casting system by creating custom cast classes, allowing you to map complex database columns (like currency amounts or geographic coordinates) to PHP value objects.

View Snippet →
PHP

Applying Application-Wide Query Constraints with Eloquent Global Scopes

Implement Eloquent global scopes to automatically apply query constraints to all queries for a specific model, useful for multi-tenancy, soft deletes, or general data filtering.

View Snippet →
PHP

Efficient Eager Loading with Conditional Constraints

Optimize Laravel Eloquent queries by eager loading relationships with specific conditions, reducing N+1 issues and memory usage for relevant data.

View Snippet →
PHP

Reusable Query Logic with Laravel Eloquent Local Scopes

Define and reuse common query constraints across your Laravel Eloquent models using local scopes, promoting DRY principles and cleaner code.

View Snippet →
PHP

Implementing Laravel Eloquent Polymorphic One-to-Many Relationships

Connect a single model to multiple other models using a polymorphic one-to-many relationship in Laravel Eloquent, ideal for versatile associations like comments or notifications.

View Snippet →
PHP

Efficient Aggregation of Related Models with Eloquent `withCount` and `withSum`

Optimize performance by retrieving aggregate values (like counts or sums) for related models without loading all relationships, directly within your main query.

View Snippet →