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 / laravel-multiformat-response.php
Last active June 24, 2018 07:42
laravel multiformat response
<?php
Response::macro('multiFormat', function ($data, $html = '') {
if (request('format') == 'json') {
return $this->json($data);
}
if (request('format') == 'xml') {
if (!is_array($data) && !$data instanceof Arrayable) { /* 400 Bad Request*/ }
<?php
$user = User::find($user_id);
$user->branches->load([
'roles' => function ($q) use ($user_id) {
$q->where('permissions_users.user_id', $user_id);
},
'roles.permissions',
]);
@jarektkaczyk
jarektkaczyk / Decorator.php
Created January 27, 2017 10:12
presenter for eloquent
<?php
namespace App\Presenters;
use ArrayAccess;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Pagination\Paginator;
class Decorator
// controller
$course = Course::with(...)->checkEstablishmentOnView()->excludeArchived()->findOrFail($id);
$view = View::make('some.view')->with('course', $course);
return response()->json(['html' => $view->render()]);
// kahlan spec
it('does some magic and unicorns and all that stuff', function () {
allow(Double::classname(['class' => App\Course::class]))->toReceive('::with')->andReturn($query = Double::instance());
@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:
//
@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 / 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 / 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 / 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))