Skip to content

Instantly share code, notes, and snippets.

@jarektkaczyk
jarektkaczyk / laravel-wheres-orWheres.php
Last active December 19, 2023 14:54
overlapping periods scope + how to nest orWhere in laravel query builder
<?php
// Freek posted a query today, that drives OhDearApp filters (the app recommended!):
// @link https://twitter.com/freekmurze/status/972299131497713664
// We can make the query more eloquent, so why don't we use a scope!
// I
// Let's make it really eloquent and get here:
$site->downtimePeriods()
@jarektkaczyk
jarektkaczyk / config.php
Last active February 23, 2023 03:26
Laravel - tinker like a boss (with PsySH)
<?php // ~/.config/psysh/config.php
// Anything not Laravel - let's try to autoload something likely to exist
if (!defined('LARAVEL_START')) {
return [
'defaultIncludes' => [
getcwd().'/vendor/autoload.php',
getcwd().'/bootstrap/autoload.php',
],
];
@jarektkaczyk
jarektkaczyk / docker-setup.md
Last active February 4, 2022 13:53
Docker setup for laravel project

ASSUMING YOU HAVE DOCKER INSTALLED ON YOUR PLATFORM

https://docs.docker.com/engine/installation/

Setup

  1. In the project root create .docker folder and docker-compose.yml file - contents below
  2. Create .docker/site.conf file with nginx setup - contents below
<?php
// meh
$mock
->method('sum')
->withConsecutive([1,2,3], [4,5,6])
->willReturnOnConsecutiveCalls(6, 15);
// instead?
$mock->method('sum')->with(1,2,3)->willReturn(6);
@jarektkaczyk
jarektkaczyk / DispatchableJobAssertion.php
Created May 19, 2020 05:05
Dispatchable incompatible with queue assertions?
<?php
// Laravel 7.*
// Testing jobs:
Queue::later(now()->addMinute(), new SomeJob);
Queue::assertPushed(SomeJob::class, fn () => ...); // works
SomeJob::dispatch()->delay(now()->addMinute());
@jarektkaczyk
jarektkaczyk / Laravel5.1-sort-collection-by-multiple-integer-fields.php
Created August 18, 2015 12:36
Laravel 5 - how to sort collection by multiple integer fields
<?php
>>> $col = collect(json_decode(json_encode([['id' => 1,'bool' => 0, 'count' => 15], ['id' => 2, 'bool' => 1, 'count' => 20], ['id' => 3, 'bool' => 0, 'count' => 10], ['id' => 4, 'bool' => 1, 'count' => 16]])))
=> Illuminate\Support\Collection {#935
all: [
{#936
+"id": 1
+"bool": 0
+"count": 15
}
@jarektkaczyk
jarektkaczyk / larave-request-vs-collection-only.php
Last active February 13, 2020 11:16
Laravel Request::only vs Collection (Arr) ::only
<?php
// given input ['name' => 'jarek']
Request::only('name', 'email')
[
"name" => "jarek",
"email" => null, // even if empty in the input, the key will be here
]
@jarektkaczyk
jarektkaczyk / github-stars-oneline.php
Last active December 13, 2019 22:25
PHP 'One-liner' to get Github stargazers count for a user
<?php
// Not exactly one-liner but.. ;)
// --------------------
// require:
// illuminate/support
// guzzlehttp/guzzle
$user = 'jarektkaczyk';
$stars = collect(json_decode((new Guzzlehttp\Client)->get("https://api.github.com/users/{$user}/repos")->getBody(), true))
@jarektkaczyk
jarektkaczyk / verbose-fatal-errors-in-artisan.php
Last active June 14, 2019 21:32
always show artisan fatal errors VERBOSE
<?php
// Add this override to your app/Exceptions/Handler.php
/**
* Render an exception to the console.
*
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @param \Exception $e
* @return void
@jarektkaczyk
jarektkaczyk / min-max.php
Created January 17, 2019 09:05
first/last/min/max on related table in Laravel Builder/Eloquent
<?php
$this->builder
->select('*') // needs to be here, otherwise the only returned column will be `last_login`
->selectSub(function ($q) {
$q->from('action_logs')
->whereColumn('user_id', 'users.id')
->selectRaw('max(created_at)');
}, 'last_login')
->get();