Skip to content

Instantly share code, notes, and snippets.

// 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 / 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
<?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 / 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*/ }
@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-regex-in-route-params.php
Last active May 15, 2018 12:20
regex in route parameters
<?php
// Do this (explicit routes are better):
Route::get('qualification', function () {/**/});
Route::get('qualification/{year}/{month}', function ($year, $month) {/**/});
// You could also do this (but don't in your case):
// @link https://laravel.com/docs/5.6/routing#parameters-regular-expression-constraints
Route::get('qualification/{year_month?}', function ($year_month = null) {
<?php
// assuming you want to get conv where last message doesn't come from the OP
Coversation::where('author_id', '!=', function ($q) {
$q->select('author_id')->from('messages')->whereRaw('conversation_id = conversations.id')->latest()->limit(1);
})->get()
<?php
$ids = array_unique([1,2,3,4,5, ...]);
// relation() is HasMany
// check if all $ids belong to this parent model:
$parent_model->relation()->whereIn('id', $ids)->count() === count($ids)
// check if $ids match all of the children records:
@jarektkaczyk
jarektkaczyk / SomeController.php
Created July 1, 2018 11:37
multi step validation in laravel
<?php
public function someController(Request $request)
{
// asumming ValidatesRequests trait in use we'll call $this->validate
$this->validate($request, [
// first set of rules
]);
@jarektkaczyk
jarektkaczyk / ExactMethodExpectation.php
Created July 16, 2018 15:24
PHPUnit - how to set expectations against exact methods calls
<?php
$mock = $this->createMock(SomeDependency::class);
$mock->expects($this->once())->method('firstMethod')->willReturn('something');
$mock->expects($this->once())->method('secondMethod')->willReturn('something else');
$cut = new SomeClass($mock)
$cut->act();
// how to ensure there's no method called other than the 2 above?