Skip to content

Instantly share code, notes, and snippets.

View mknparreira's full-sized avatar

Maikon Parreira mknparreira

View GitHub Profile
@mknparreira
mknparreira / postegre-with-recursive.md
Last active August 12, 2021 22:16
PostegreSQL | With Recursive
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
@mknparreira
mknparreira / laravel-collection-group-with-sum-values.md
Last active August 12, 2021 22:15
Laravel | Collection group with sum values
$mystuff = collect($something);
$num = $mystuff->groupBy('dateDay')->map(function ($row) {
    return $row->sum('n');
});

dd($num);
@mknparreira
mknparreira / lumen-transactions-multiple-connections.md
Last active August 12, 2021 22:14
Lumen | How to use Transactions with multiple connections
        $cn = DB::connection('pgsql-private');
        $cn->beginTransaction();

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

            if (! $create) {
@mknparreira
mknparreira / laravel-all-months-with-two-dates-carbon.md
Last active August 12, 2021 22:13
Laravel | Getting all months between 2 dates Carbon
public function getMonthListFromDate(Carbon $start)
{
    $start = $start->startOfMonth();
    $end   = Carbon::today()->startOfMonth();

    do
    {
 $months[$start->format('m-Y')] = $start->format('F Y');
@mknparreira
mknparreira / javascript-remove-object-array-of-objects.md
Last active August 12, 2021 22:12
Javascript | If you want to remove one object using only the id property

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 / javascript-replacing-objects-array.md
Last active September 21, 2023 14:03
Javascript | Replacing objects in array (Updating) JavaScript

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 / typescript-create-decorators.md
Last active August 12, 2021 22:10
Typescript | Simple way to create decorators with TypeScript

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 / postegre-row-to-json-with-nested-joins.md
Last active August 12, 2021 22:09
PostgreSQL | 9.2 row_to_json() with nested joins
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 / postegre-row-to-json-example.md
Last active August 12, 2021 22:08
PostegreSQL | row_to_json example
SELECT json_agg(row_to_json(ROW)) AS treeeeeee 
FROM (
   SELECT * FROM table_name					
) row
@mknparreira
mknparreira / laravel-check-if-update-or-create-perfomed-update.md
Last active August 12, 2021 22:07
Laravel | check if updateOrCreate performed update
$tourist = Tourist::updateOrCreate([...]);

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

if(!$tourist->wasRecentlyCreated && !$tourist->wasChanged()){
    // updateOrCreate performed nothing, row did not change
}