Premium
PHP Snippets.

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

PHP

Secure Password Hashing with Bcrypt in PHP

Learn to securely hash and verify user passwords in PHP using the robust `password_hash()` and `password_verify()` functions with the Bcrypt algorithm, enhancing your application's security.

View Snippet →
PHP

Server-Side Input Sanitization for XSS Prevention in PHP

Learn to effectively sanitize user-generated input on the server-side using PHP's `htmlspecialchars()` function, preventing Cross-Site Scripting (XSS) vulnerabilities before displaying data.

View Snippet →
PHP

Caching External API Responses in PHP with TTL

Implement a basic caching mechanism in PHP to store external API responses, reducing redundant requests and improving application performance with a Time-To-Live.

View Snippet →
PHP

Defining Accessors and Mutators in Laravel Eloquent

Customize how your Eloquent model attributes are retrieved and saved by defining accessors for formatting data on retrieval and mutators for transforming data before saving.

View Snippet →
PHP

Using Model Observers for Centralized Event Handling in Eloquent

Learn to use Laravel Eloquent Observers to centralize event listeners for models, making your code cleaner and more organized by separating event logic.

View Snippet →
PHP

Performing Batch Updates and Deletes with Laravel Eloquent

Learn how to efficiently update or delete multiple records in Laravel Eloquent without retrieving each model individually, significantly improving performance for bulk operations.

View Snippet →
PHP

Using `firstOrCreate`, `firstOrNew`, `updateOrCreate` for Atomic Operations

Master Laravel Eloquent's `firstOrCreate`, `firstOrNew`, and `updateOrCreate` methods for atomically retrieving, creating, or updating records, preventing race conditions.

View Snippet →
PHP

Efficient Eager Loading with Relationship Constraints

Optimize Laravel Eloquent N+1 issues by eagerly loading relationships while applying specific conditions to the loaded related models, improving query performance.

View Snippet →
PHP

Applying Global Query Scopes in Eloquent

Automatically apply query constraints across multiple Eloquent queries for a model using global scopes, ensuring consistent data filtering throughout your application.

View Snippet →
PHP

Implementing Polymorphic Many-to-Many Relationships

Structure databases and models for polymorphic many-to-many relationships, allowing a single model to be associated with multiple different model types via a pivot table.

View Snippet →
PHP

Creating Custom Attribute Casts in Eloquent

Transform Eloquent model attributes to and from custom PHP objects or specific formats using advanced custom casting logic for complex data types.

View Snippet →
PHP

Querying JSON Column Data in Eloquent

Perform advanced queries on JSON data stored in database columns directly with Laravel Eloquent, using dot notation for nested values and various comparison operators.

View Snippet →