Skip to content

Instantly share code, notes, and snippets.

@hightemp
Last active October 6, 2018 14:34
Show Gist options
  • Save hightemp/d58ff8394358dcf0d338d6056b728ac3 to your computer and use it in GitHub Desktop.
Save hightemp/d58ff8394358dcf0d338d6056b728ac3 to your computer and use it in GitHub Desktop.
Laravel snippets

Laravel snippets

Define PDO object in DB Laravel

DB::connection()->setPdo($pdo);

Get PDO object from DB Laravel

$pdo = DB::connection()->getPdo();

Get character in UTF8 by code (mb_chr)

function fnUnichr($i) 
{
  return iconv('UCS-4LE', 'UTF-8', pack('V', $i));
}

Get char code from UTF8 string (mb_ord)

function fnUniord($s) 
{
  return unpack('V', iconv('UTF-8', 'UCS-4LE', $s))[1];
}

Split UTF8 string into characters array

function mb_str_split($sString) { 
  return preg_split('/(?<!^)(?!$)/u', $sString); 
} 

Detects console (cli)

if (app()->runningInConsole()) {
  // do something
}

Validate console input

use Illuminate\Console\Command;
use Validator;

class NewCommand extends Command
{
    protected $description = '';
    protected $signature = 'new_command';

    public function fnValidate($oMethod, $aRules)
    {
        $sValue = $oMethod();
        $bValidate = $this->fnValidateInput($aRules, $sValue);

        if ($bValidate !== true) {
            $this->warn($bValidate);
            $sValue = $this->fnValidate($oMethod, $aRules);
        }
        return $sValue;
    }

    public function fnValidateInput($aRules, $sValue)
    {
        $oValidator = Validator::make([$aRules[0] => $sValue], [$aRules[0] => $aRules[1]], trans('validation'));

        if ($oValidator->fails()) {
            $oError = $oValidator->errors();
            return $oError->first($aRules[0]);
        }else{
            return true;
        }
    }

    public function handle()
    {
      $sParameter = $this->fnValidate(function() {
          return $this->ask('Something to ask', false);
      }, ['sParameter', 'required|min:10|regex:/[a-zA-Z0-9]+/']);
    }
}

Artisan::call('migrate') raises memory limit exception

Artisan::call('migrate', [ '--force' => true ]);

Random string

Method 1

$random = str_random(40);

Method 2

function fnRandomString($iLength) 
{
  $aKeys = array_merge(range(0, 9), range('a', 'z'));

  $sKey = "";
  for($iIndex=0; $iIndex < $iLength; $iIndex++) {
    $sKey .= $aKeys[mt_rand(0, count($aKeys) - 1)];
  }
  return $sKey;
}

Can't catch exceptions with try catch block

// In namespace use \
try {

} catch(\Exception $oException) {

}

Access request object

Method 1

Request::method();

Method 2

use Illuminate\Http\Request;

class UserController extends Controller
{
  public function action(Request $request)
  {
      //
  }
}

Method 3

// The request function returns the current request instance or obtains an input item:
$request = request();
$value = request('key', $default);

Call controller action

Method 1

return App::call('App\Http\Controllers\Controller@callAction', ['action', $aParameters]);

Method 2

$oController = app()->make('Controller');
return $oController->callAction('action', $aParameters);

Catch any route

Route::any(
  '{any}', 
  function () 
  {
    // TODO
  }
)->where('any', '.*');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment