Skip to content

Instantly share code, notes, and snippets.

@wsydney76
wsydney76 / ELEMENTQUERIES_IN_LARAVEL.md
Last active November 9, 2026 11:19
Craft 6 Element Queries With Laravel Query Builder

Craft 6 Element Queries With Laravel Query Builder

Craft 6 element queries are built on Laravel's query builder, providing access to Laravel's query methods alongside Craft's element-specific query methods.

This document collects practical examples, differences from Craft 5 and native Laravel, and things to watch out for when working with element queries in Craft 6. It covers filtering, ordering, retrieving and paginating elements, working with custom fields, and more advanced techniques such as reusable query conditions and raw SQL.

The focus is on PHP and Blade, but most concepts also apply to Twig when using helper functions that return element queries.

| PHP | Twig |

@ryanleichty
ryanleichty / related-entries.twig
Created August 6, 2025 13:46
Related Entries
@croxton
croxton / relatedEntries.twig
Last active September 11, 2025 10:45
Craft CMS 5 - display related entries on a 'Used by' tab, using a custom template UI element
{% set rels = craft.entries.relatedTo({ targetElement: element }).siteId('*').collect() %}
{% set owners = [] %}
{% for rel in rels %}
{% set owner = rel.owner ?? rel %}
{% set owner = owner.primaryOwner ?? owner %}
{% if owner.canonicalId not in owners %}
{% set owners = owners + {
(""~owner.canonicalId~"") : {
cpEditUrl : owner.cpEditUrl,
<?php
namespace modules;
use Craft;
use craft\controllers\EntriesController;
use craft\controllers\UsersController;
use yii\base\ActionEvent;
use yii\base\Event;
use yii\base\Module;
use yii\web\ForbiddenHttpException;
@croxton
croxton / .env.php
Last active March 20, 2019 19:23
Craft 3 live previews across different root (or sub) domains
<?php
// In general.php define an alias:
'aliases' => [
'@baseUrl' => getenv('CRAFTENV_BASE_URL')
],
// In the CP, set the Base URL for each site to @baseUrl/
// When previewing, the CP will therefore always use the hostname the editor logged into as the base url for the preview
// (that happens with 'new' entries anyway regardless of base url, but existing entries use base url).
@nfourtythree
nfourtythree / Password Protect Serverpilot App.md
Last active February 6, 2025 21:58
Password Protect Serverpilot App

How to password protect your Serverpilot app

This is a quick (rough) guide on how to protect a Serverpilot pilot app with auth basic

1. SSH into your Serverpilot Server

ssh SERVERPILOTUSERNAME@your.server.ip.address

2. Create a htpasswd file in your app

@nfourtythree
nfourtythree / How To Disable Strict SQL Mode in MySQL 5.7.MD
Last active April 29, 2021 09:45
Serverpilot - Disable Strict SQL Mode in MySQL 5.7

How To Disable Strict SQL Mode in MySQL 5.7

I wrote a quick script for those that provision servers often enough.

To get this to work take the following steps

  1. Log into your server as root
  2. Get script wget https://gist.githubusercontent.com/nfourtythree/90fb8ef5eeafdf478f522720314c60bd/raw/disable-strict-mode.sh
  3. Make is executable chmod +x disable-strict-mode.sh
  4. Run the script ./disable-strict-mode.sh
@khalwat
khalwat / crontab_helper
Last active April 20, 2018 23:39
Never struggle with editing your crontab again; put this in the beginning of each of your crontabs
This is now part of craft-scripts:
https://github.com/nystudio107/craft-scripts
@joshangell
joshangell / MyPluginController.php
Last active February 1, 2016 14:07
Content migration in Craft example.
<?php
namespace Craft;
class MyPluginController extends BaseController
{
// This lets anyone run the controller actions we specify, useful for CRON etc
protected $allowAnonymous = array('actionMigrate');
/**