Skip to content

Instantly share code, notes, and snippets.

@jarektkaczyk
jarektkaczyk / gist:8d3e1f34e6e6c6970173
Last active August 5, 2016 02:06
Laravel5 helpers for tinkering with your app
<?php
/**
* Print query with bindings instead of '?'
*/
function toRaw($query)
{
return vsprintf(str_replace('?', "'%s'", $query->toSql()), $query->getBindings());
}
@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 / gist:d0f89852205ec1ab69cb
Last active March 18, 2016 23:06
how to remove given child component from parent's array (including dynamically added elements) ?
<div id="some-id">
<category v-for="category in categories"
:id="category.id"
:name.sync="category.name"
</category>
// we add items to the categories array dynamically
<button @click="categories.push({})">Add categories</button>
</div>
@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 / 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 / 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
@jarektkaczyk
jarektkaczyk / laravel-tinker-RouteList-helper.php
Created September 8, 2016 02:49
Handy helper for working with route list in tinker [Laravel]
<?php
// Easily get route list in tinker
>>> app('RouteList')->setOption('method', 'PUT')->getRoutes()
>>> app('RouteList')->getRoutes()->where('name', 'website.login')
class RouteList extends \Illuminate\Foundation\Console\RouteListCommand
{
/** @var array */
@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 / tags.php
Last active May 6, 2018 08:11 — forked from AdrianKuriata/tags.php
This is optimization tags for ManyToMany relationship with Post model for attaching not existing tags.
public function store(Post $post)
{
$tags = [];
foreach ((array) request('tags') as $tag_name) {
$tags[] = Tag::firstOrNew(['name' => $tag_name]);
}
// a dla fanow adama wathana o jedna linijke mniej:
//