PHP Classes

File: helpers.php

Recommend this page to a friend!
  Classes of Kevinralph M Tenorio   Laravel Helpers   helpers.php   Download  
File: helpers.php
Role: Auxiliary script
Content type: text/plain
Description: helpers
Class: Laravel Helpers
Database access helpers using Laravel Eloquent
Author: By
Last change: - Added `pdo_make_factory`
Date: 6 years ago
Size: 2,720 bytes
 

Contents

Class file image Download
<?php

if (!function_exists('parseSqlQuery')) {
   
/**
     * Get raw Sql
     *
     * @example parseSqlQuery(User::where('name', 'foo'));
     * @param \Illuminate\Database\Eloquent\Builder $builder
     * @return string
     */
   
function parseSqlQuery(Builder $builder)
    {
       
$sql = $builder->toSql();

        foreach (
$builder->getBindings() as $binding) {
           
$value = is_numeric($binding) ? $binding : "'" . $binding . "'";
           
$sql = preg_replace('/\?/', $value, $sql, 1);
        }

        return
$sql;
    }
}

if (!
function_exists('pdo_make_factory')) {
   
/**
     * Create PDO Factory
     *
     * @param array|string|null $config
     * @return \Illuminate\Database\Connection
     */
   
function pdo_make_factory($config = null)
    {
       
$sid = 'api_pdo_factory.instance';

        if (
is_null($factory = session($sid))) {

           
$cfg_name = \DB::getDefaultConnection();
           
$cfg_env = collect(config('database.connections.' . $cfg_name, []));

            if (
$config && is_string($config)) {
               
$cfg_name = $config;
            }

            if (
$config && is_array($config)) {
               
$cfg_env = $cfg_env->merge($config);
            }

           
session($sid, $factory = app('db.factory')->make(['database' => null, 'strict' => false] + $cfg_env->all()));
        }

        return
$factory;
    }
}

if (!
function_exists('pdo_statement')) {
   
/**
     * Run a PDO Statement
     *
     * @param string $statement
     * @param array|string|null $config
     * @return mixed
     * @throws \Exception
     */
   
function pdo_statement($statement, $config = null)
    {
        if (!
$factory = pdo_make_factory($config)) {
            throw new \
Exception('Invalid connection factory.');
        }

        return
$factory->select($statement);
    }
}

if (!
function_exists('db_exists')) {
   
/**
     * Check if database exists
     *
     * @param string $name
     * @return bool
     * @throws \Exception
     */
   
function db_exists($name)
    {
        if (
$name && !empty($name)) {
           
$statement = "SELECT IF('{$name}' IN(SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA), 1, 0) as found";
           
$result = pdo_statement($statement);
            return
$result[0]->found === 1;
        } else {
            throw new \
Exception("Invalid Database Name `{$name}`");
        }
    }
}

if (!
function_exists('prop')) {
   
/**
     * Alternative to Collection
     *
     * @param $data
     * @return ObjectProp
     */
   
function prop($data)
    {
        return new
ObjectProp($data);
    }
}