Skip to content

Instantly share code, notes, and snippets.

Avatar

Maikon Parreira mknparreira

View GitHub Profile
@mknparreira
mknparreira / laravel_check_if_update_or_create_perfomed_update.md
Created Jan 20, 2021
Laravel check if updateOrCreate performed update
View laravel_check_if_update_or_create_perfomed_update.md
$tourist = Tourist::updateOrCreate([...]);

if(!$tourist->wasRecentlyCreated && $tourist->wasChanged()){
    // updateOrCreate performed an update
}

if(!$tourist->wasRecentlyCreated && !$tourist->wasChanged()){
    // updateOrCreate performed nothing, row did not change
}
View row_to_json_example.md
SELECT json_agg(row_to_json(ROW)) AS treeeeeee 
FROM (
   SELECT * FROM table_name					
) row
@mknparreira
mknparreira / row_to_json_postgreSQL.md
Last active Nov 19, 2020
PostgreSQL 9.2 row_to_json() with nested joins
View row_to_json_postgreSQL.md
SELECT
        json_build_object(
                'id', u.id,
                'name', u.name,
                'email', u.email,
                'user_role_id', u.user_role_id,
                'user_role', json_build_object(
                        'id', ur.id,
@mknparreira
mknparreira / create-decorators-typescript.md
Last active Sep 28, 2020
Simple way to create decorators with TypeScript
View create-decorators-typescript.md

Don´t forget to set up experimentalDecorators as a true value into the compileOptions attribute in the tsconfig.json file.

Call this decorator before method as @decoratorName()

export function decoratorName(paramName : boolean = false) {

    return function(target: any, propertyKey: string, descriptor: PropertyDescriptor) {

        const originalMethod = descriptor.value;
        
@mknparreira
mknparreira / replacing-objects-array-javascript.md
Created Sep 19, 2020
Replacing objects in array (Updating) JavaScript
View replacing-objects-array-javascript.md

I need to replace objects in arr1 with items from arr2 with same id

var arr1 = [
  { id: '124', name: 'qqq'}, 
  { id: '589', name: 'www'}, 
  { id: '45', name: 'eee'}, 
  { id: '567', name: 'rrr'}
];
@mknparreira
mknparreira / remove-object-array-of-objects-javascript.md
Last active Sep 19, 2020
If you want to remove one object using only the id property
View remove-object-array-of-objects-javascript.md

If you want to remove one object using only the id property

## I need to replace objects in arr1 with items from arr2 with same id


const apps = [{id:34,name:'My App',another:'thing'},{id:37,name:'My New App',another:'things'}];

var removeIndex = apps.map(function(item) { return item.id; }).indexOf(37);
@mknparreira
mknparreira / all-months-with-two-dates-carbon.md
Created Jul 28, 2020
Getting all months between 2 dates Carbon
View all-months-with-two-dates-carbon.md
<?php

public function getMonthListFromDate(Carbon $start)
{
    $start = $start->startOfMonth();
    $end   = Carbon::today()->startOfMonth();

    do
    {
@mknparreira
mknparreira / transactions-multiple-connections-lumen.md
Last active Jul 28, 2020
How to use Transactions with multiple connections on Lumen
View transactions-multiple-connections-lumen.md
        $cn = DB::connection('pgsql-private');
        $cn->beginTransaction();

        try {
            $create = $this->create([
                'fk_user_id' => $user_id,
            ]);

            if (! $create) {
@mknparreira
mknparreira / laravel-collection-group-with-sum-values.md
Last active Mar 6, 2020
Laravel Collection group with sum values
View laravel-collection-group-with-sum-values.md
<?php
$mystuff = collect($something);
$num = $mystuff->groupBy('dateDay')->map(function ($row) {
    return $row->sum('n');
});

dd($num);
@mknparreira
mknparreira / with-recursive-postegreSQL.md
Last active Nov 23, 2020
With Recursive - PostegreSQL
View with-recursive-postegreSQL.md
WITH RECURSIVE supplytree AS
(
	SELECT 
		id,
		description, 
		parent_feature_id,
		to_jsonb(feature) AS si_object,
		CAST(description As varchar(1000)) AS si_fullname 
	FROM feature