// Built-in types
$variable = null; // null type
// Scalar types
$boolVar = true; // bool type
$intVar = 42; // int type
$floatVar = 3.14; // float type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * TimeoutGuard — A lightweight timeout wrapper using PHP ticks. | |
| * | |
| * This lets you interrupt execution of any callable if it exceeds a time limit. | |
| * Works even on Windows (unlike pcntl). | |
| * | |
| * ⚠️ Requires `declare(ticks=1);` | |
| */ |
A clean and categorized reference of the most useful constants in PHP.
Includes descriptions and sample outputs for context. Fully valid for PHP 8.3 and safe for PHP 8.4.
| Constant | Description | Example Output |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Providers; | |
| use Illuminate\Database\Eloquent\Builder; | |
| use Illuminate\Support\Arr; | |
| use Illuminate\Support\ServiceProvider; | |
| class AppServiceProvider extends ServiceProvider | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Traits\Models; | |
| use App\Models\Team; | |
| use Illuminate\Database\Eloquent\Builder; | |
| trait BelongsToTeam | |
| { | |
| // This method is executed when a new model is being created. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| Artisan::command('generate-tests', function () { | |
| // Define the base path where class files are located | |
| $files = File::allFiles(base_path('app/Classes')); | |
| foreach ($files as $file) { | |
| // Get the relative path of the current class file and apply string manipulations | |
| $path = str($file->getRelativePathname()) | |
| ->replace('.php', '') // Remove the .php extension | |
| ->replace('/', '\\') // Replace directory slashes with namespace slashes |
- Laravel's model scopes don't work with Scout (search).
- Reusing scopes across models means copying code OR using traits (but Scout still breaks).
- Leads to duplicate query logic.
Instead of defining scopes inside models, create scope classes that can be used in both Eloquent & Scout queries!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // In Laravel, we have the $with property to automatically load a relationship every time. | |
| // However, selecting a specific column by default for every model query doesn't seem possible... | |
| // but here's a hack I found! | |
| class Post extends Model | |
| { | |
| // To automatically load the 'author' relationship on every query | |
| protected $with = ['author']; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Models; | |
| use Illuminate\Database\Eloquent\Factories\HasFactory; | |
| use Illuminate\Database\Eloquent\Model; | |
| use Illuminate\Support\Facades\DB; | |
| class Blog extends Model | |
| { |
NewerOlder