PHP code example of zlt / lara-calls

1. Go to this page and download the library: Download zlt/lara-calls library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zlt / lara-calls example snippets


return [
    /*
     * Define your eloquent builder class so that you can use custom Eloquent macros provided by package.
     */
    'builder' => \Illuminate\Database\Eloquent\Builder::class,

    /*
     * You may customize which macros you don't want to use.
     * Only macros provided below will not be registered.
     * Available Macros are 'onlyValues','pluckMultiple','updateOrCreateWhen','sortInValue','sortInValueDesc','groupAndSortBy','groupAndSortByDesc','calc_exec_time','validation'.
     */
    'exclude_macros' => [],
];

  $collection = collect([
            [
                'a' => 123,
                'b' => 12,
            ],
            [
                'a' => 12,
                'b' => 31,
            ]
        ]);
  dd($collection->onlyValues()->toArray()); // [ [123,12],[12,31] ]; 
   

  // $collection->pluckMultiple(array $keys);
  $collection = collect([
            [
                'a' => 123,
                'b' => 12,
            ],
            [
                'a' => 12,
                'b' => 31,
                'c' => 32,
            ]
        ]);
   
  dd($collection->pluckMultiple(['a', 'b']));
  /*
  Illuminate\Support\Collection {
    all: [
           [ "a" => 123, "b" => 12,],
           [ "a" => 12, "b" => 31,],
      ],
  }
  */
  
  $collection = collect([
            [
                'a' => 123,
                'b' => 12,
                'c' => [
                    'x' => 1,
                ]
            ],
            [
                'a' => 12,
                'b' => 31,
                'c' => [
                    'x' => 2,
                ],
            ]
        ]);
   //pluck nested attribute 
  dd($collection->pluckMultiple(['a', 'c.x']));
  /*
  Illuminate\Support\Collection {
     all: [
            [ "a" => 123, "c.x" => 1,],
            [ "a" => 12, "c.x" => 2,],
          ],
       }
   */
   

  // $collection->sortInValue(string $attributeToSort);
  // Sometimes you may sort attribute values by key of that attribute values. Then use $collection->sortInValue(string $attributeToSort,string $someKeyOfThatAttribute);
   $collection = collect([
          [
           'a' => [4,5,1,3]
          ],
          [
            'a' => [5,6,1,8]
          ]
    ]);
    $collection->sortInValue('a');
  /*
  Illuminate\Support\Collection {#4325
     all: [
       [
         "a" => Illuminate\Support\Collection {#4328
           all: [
             2 => 1,
             3 => 3,
             0 => 4,
             1 => 5,
           ],
         },
       ],
       [
         "a" => Illuminate\Support\Collection {#4322
           all: [
             2 => 1,
             0 => 5,
             1 => 6,
             3 => 8,
           ],
         },
       ],
     ],
   }
  */
   
  $collection = collect([
            [
                'a' => [
                    ['b' => 4],
                    ['b' => 5],
                    ['b' => 1],
                    ['b' => 3]]
            ],
            [
                'a' => [
                    ['b' => 5],
                    ['b' => 2],
                    ['b' => 6],
                    ['b' => 1]]
            ]
        ]);
    $collection->sortInValue('a','b'); // sort 'a' values by 'b' key of 'a'
  /*
  Illuminate\Support\Collection {#4348
     all: [
       [
         "a" => Illuminate\Support\Collection {#4347
           all: [
             2 => [
               "b" => 1,
             ],
             3 => [
               "b" => 3,
             ],
             0 => [
               "b" => 4,
             ],
             1 => [
               "b" => 5,
             ],
           ],
         },
       ],
       [
         "a" => Illuminate\Support\Collection {#4346
           all: [
             3 => [
               "b" => 1,
             ],
             1 => [
               "b" => 2,
             ],
             0 => [
               "b" => 5,
             ],
             2 => [
               "b" => 6,
             ],
           ],
         },
       ],
     ],
   }
  */  
    

  //$collection->groupAndSortBy(string $attributeKeyToGroup,string $attributeKeyToSortAfterGroup,Closure $closureToAdjustSortedCollection);
   $collection = collect([
            [
                'name' => 'James',
                'age' => 20,
            ],
            [
                'name' => 'Watson',
                'age' => 24,
            ],
            [
                'name' => 'James',
                'age' => 15,
            ]
        ]);

   dd($collection->groupAndSortBy('name','age')); 
  /*
   Illuminate\Support\Collection^ {#4321
             #items: array:2 [
         "James" => Illuminate\Support\Collection^ {#4322
           #items: array:2 [
             1 => array:2 [
               "name" => "James"
               "age" => 15
             ]
             0 => array:2 [
               "name" => "James"
               "age" => 20
             ]
           ]
         }
         "Watson" => Illuminate\Support\Collection^ {#4325
           #items: array:1 [
             0 => array:2 [
               "name" => "Watson"
               "age" => 24
             ]
           ]
         }
       ]
    }
    */
  
   $collection = collect([
            [
                'name' => 'James',
                'age' => 20,
            ],
            [
                'name' => 'Watson',
                'age' => 24,
            ],
            [
                'name' => 'James',
                'age' => 15,
            ]
        ]);

   dd($collection->groupAndSortBy('name','age',function($collection){
       return $collection->where('age','>=',24);
   })); // after group and sorted, adjust the sorted collection
        /*
   Illuminate\Support\Collection^ {#4313
       #items: array:2 [
         "James" => Illuminate\Support\Collection^ {#4311
           #items: []
         }
         "Watson" => Illuminate\Support\Collection^ {#4309
           #items: array:1 [
             0 => array:2 [
               "name" => "Watson"
               "age" => 24
             ]
           ]
         }
       ]
     }
    */
  

  //$collection->validation(array|Closure $rulesOrClosure)->onSuccess(Closure $closure)->onError(Closure $closure);
  $collection = collect(['name'=>'John','email'=>'[email protected]']);
  
  $collection->validation(['name'=>'string','email'=>'email'])
               ->onSuccess(function($collection){
                  // This will be processed              
                  return $collection;
               })->onError(function($messageBag){
                  // This will be skipped
                  return $messageBag->toArray();
               });
  
  $collection = collect(['name'=>'John','email'=>'email']);
  
  $collection->validation(['name'=>'string','email'=>'email'])
               ->onSuccess(function($collection){
                  //This step will be skipped.                
                  return $collection;
               })->onError(function($messageBag){
                  return $messageBag->toArray();
               }); // [ "email" => [ "The email must be a valid email address.", ], ]
  
  $collection->validation(function($collection){
                  return false; //Return type must be 'boolean'.Otherwise, it will always return false.})
               ->onSuccess(function($collection){
                  //This step will be skipped.
                  return $collection;
               })->onError(function(){
                 //Perform some process after failing validation
               });
    

    $food = Food::updateOrCreateWhen(['name' => 'sushi',], [
        'price' => 100,
    ]);
    
    dd($food->price); //100

    $food = Food::updateOrCreateWhen(['name' => 'sushi'], [
        'price' => 200
    ], function ($oldValue, $newValue) {
        return true;
    });

    dd($food->price); //200;

    $food = Food::updateOrCreateWhen(['name' => 'sushi'], [
        'price' => 300
    ], function ($oldValue, $newValue) {
        return false;
    });

    dd($food->price); //200;
    

   $time = calc_exec_time(function(){
   sleep(5);
  return 'test';
  });
   dd($time); //"5.0008"
    
bash
php artisan vendor:publish --provider="Zlt\LaraCalls\LaraCallsServiceProvider"