Premium
PHP Snippets.

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

PHP

Extract Unique Values from Array of Associative Arrays by Key

Discover how to efficiently extract a list of unique values for a specific key from an array where each element is an associative array or object.

View Snippet →
PHP

OAuth 2.0 Client Credentials Grant for API Access

Learn to securely obtain an access token using the OAuth 2.0 Client Credentials grant type for server-to-server API authentication with Guzzle HTTP client in PHP.

View Snippet →
PHP

Filtering Parent Models Based on Related Model Attributes with Eloquent `whereHas`

Learn to efficiently filter your parent models in Laravel Eloquent by applying conditions on their related models using the `whereHas` method.

View Snippet →
PHP

Implementing Global Query Constraints with Laravel Eloquent Global Scopes

Master applying default, site-wide query constraints to your Laravel Eloquent models automatically using Global Scopes, ideal for multi-tenancy or soft deletes.

View Snippet →
PHP

Implementing Flexible Polymorphic Relationships with Laravel Eloquent

Discover how to build versatile one-to-many or many-to-many relationships where a model can belong to multiple other models on a single association.

View Snippet →
PHP

Handling Model Lifecycle Events with Eloquent Observers

Learn to execute custom logic automatically when Eloquent models are created, updated, deleted, or retrieved using model events and dedicated observers.

View Snippet →
PHP

Sanitize Input by Stripping HTML Tags

A PHP regex snippet to effectively remove all HTML tags from a string, preventing XSS vulnerabilities in user-generated content.

View Snippet →
PHP

Optimizing Eager Loading by Selecting Specific Columns and Nested Relationships

Learn how to enhance application performance in Laravel Eloquent by eagerly loading only necessary columns from relationships, including nested ones, to reduce memory usage.

View Snippet →
PHP

Efficiently Creating or Updating Records with Eloquent `updateOrCreate`

Master Laravel Eloquent's `updateOrCreate` method to atomically insert a new record or update an existing one based on specified attributes, simplifying data synchronization.

View Snippet →
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 →