Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 */
// 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 / 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 / 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: