Premium
PHP Snippets.

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

PHP

Create Reusable Local Scopes in Laravel Eloquent

Discover how to define local scopes in Laravel Eloquent to encapsulate common query constraints, making your code more readable and reusable across different parts of your application.

View Snippet →
PHP

Define Eloquent Accessors and Mutators for Attribute Handling

Customize how model attributes are retrieved and set in Laravel Eloquent using accessors and mutators. Perfect for formatting data or performing transformations automatically.

View Snippet →
PHP

Filter Models Based on Related Model Existence with `whereHas`

Learn to use Laravel Eloquent's `whereHas` method to filter parent models based on conditions of their related models, enabling complex data retrieval.

View Snippet →
PHP

Efficiently Eager Load Eloquent Relationships to Prevent N+1 Queries

Learn how to use Eloquent's `with()` method to eager load related models, drastically improving performance by preventing the common N+1 query problem in Laravel applications.

View Snippet →
PHP

Create or Update Eloquent Records with `updateOrCreate`

Discover Laravel's `updateOrCreate` method for Eloquent models, allowing you to elegantly insert a new record or update an existing one based on specified attributes.

View Snippet →
PHP

Implement CSRF Protection with Synchronizer Tokens in PHP

Secure web forms against Cross-Site Request Forgery (CSRF) attacks by generating and validating unique synchronizer tokens for each user session.

View Snippet →
PHP

Prevent Cross-Site Scripting (XSS) with HTML Output Escaping

Securely display user-generated content on web pages by properly escaping HTML entities, preventing malicious scripts from executing and safeguarding user data.

View Snippet →
PHP

Configure Secure and HttpOnly Cookies for Enhanced Session Security

Strengthen web application security by setting HttpOnly and Secure flags on session cookies, protecting against XSS attacks and interception over insecure connections.

View Snippet →
PHP

Prevent SQL Injection with Parameterized Queries in PHP PDO

Safeguard databases from SQL injection attacks by using prepared statements and parameterized queries, ensuring user input is treated as data, not executable code.

View Snippet →
PHP

Securely Hash and Verify Passwords with Bcrypt in PHP

Implement robust password security by using PHP's built-in `password_hash()` and `password_verify()` functions with Bcrypt, safeguarding user credentials against brute-force and rainbow table attacks.

View Snippet →
PHP

Implement a Basic FIFO Queue Using PHP Array Functions

Learn to build a First-In-First-Out (FIFO) queue data structure in PHP using `array_push` and `array_shift` for efficient task processing.

View Snippet →
PHP

Generate Cartesian Product of Multiple PHP Arrays

Efficiently generate all possible combinations (Cartesian product) from several PHP arrays, useful for product variations or test data generation.

View Snippet →