Premium
PHP Snippets.

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

PHP

Extracting a Column from a Multi-dimensional Array

Learn how to use PHP's `array_column` function to easily retrieve all values from a single column of a multi-dimensional array or an array of objects.

View Snippet →
PHP

Managing Diverse Related Models with Polymorphic Relationships

Learn to implement Laravel Eloquent polymorphic relationships for models that can belong to multiple different types of parent models, simplifying data structures and queries.

View Snippet →
PHP

Enhancing Many-to-Many Relationships with Custom Pivot Models

Discover how to define and interact with a custom pivot model in Laravel Eloquent, allowing you to add extra attributes and custom logic to your many-to-many relationship.

View Snippet →
PHP

Implementing Global Scopes for Application-Wide Eloquent Filtering

Apply consistent query constraints across multiple models automatically using Laravel Eloquent global scopes, ensuring data integrity and simplifying common query patterns.

View Snippet →
PHP

Using Eloquent Subquery Selects for Advanced Aggregations

Optimize complex queries by embedding subqueries directly into your Laravel Eloquent selects to retrieve related aggregate data or latest values efficiently in a single query.

View Snippet →
PHP

Automating Actions with Laravel Eloquent Model Observers

Implement robust logic before or after model events like creating, updating, or deleting using Laravel Eloquent Observers, centralizing and organizing model lifecycle actions.

View Snippet →
PHP

Server-Side HTML Encoding to Prevent XSS on Display

Prevent Cross-Site Scripting (XSS) attacks by properly HTML encoding user-generated content on the server-side before displaying it in your PHP web application.

View Snippet →
PHP

Efficiently Eager Load Multiple Eloquent Relationships with Constraints

Optimize database queries by eager loading multiple related models with specific conditions, preventing N+1 problems and improving application performance.

View Snippet →
PHP

Implement and Query Eloquent Soft Deletes

Learn to implement soft deletes in Laravel Eloquent models, allowing records to be 'deleted' without actual database removal, and how to query, restore, or force delete them.

View Snippet →
PHP

Define Accessors and Mutators with Laravel 9+ Attributes

Transform model attribute values on retrieval and before saving using Laravel 9+'s Attribute casting, enhancing data presentation and manipulation within your application.

View Snippet →
PHP

Perform Batch Updates on Eloquent Models

Learn to perform efficient batch updates on multiple Eloquent records using the query builder's `update()` method, avoiding individual model hydration for better performance.

View Snippet →
PHP

Query and Manipulate JSON Columns with Laravel Eloquent

Efficiently query and manipulate JSON data stored in database columns using Laravel Eloquent's built-in methods like `whereJsonContains` and `whereJsonLength`.

View Snippet →