<?php
/**
* When we want to perform a bulk insert we will not found support for this action in Eloquent or Query Builder
* To avoid performance issues i have to drop my preference to be database agnostic in my Apps
* To be used like:
* $table = 'table_name';
* $columns = array($column1, $column2, $column3);
* $values = array(
* array('Paulo', 'Silva', 'Exadra37'),
* array('Jhon', 'Doe', 'JD'),
* array('Mary', 'Doe', 'MD')
* );
* $this->insertOnDuplicateKeyUpdate($table, $columns, $values)
*
* @author Exadra37(Paulo Silva) <exadra37atgmailpointcom>
* @since 2015/03/26
* @link https://gist.github.com/Exadra37/fd8ebf60e5ffa59e803b#file-laravel-tips-md
*
* @param string $table - database table name
* @param array $columns - array of database columns names
* @param array $values - array of values to be used in bindings
* @return boolean
*/
public function insertOnDuplicateKeyUpdate($table, array $columns, array $values)
{
$columns = array('master_product_id', 'product_id', 'price');
$values = array(
array('le23', '232', '45'),
array('le23', '30', '320.3'),
array('le50', '952', '12.50')
);
$bindings = array();
$countColumns = count($columns);
$tableColumns = '`'.implode('`,`', $columns).'`';
$bindsPlaceholders = rtrim(str_repeat('?,', $countColumns), ',');
/**
* INSERT INTO reviews (
* column1,
* column2,
* column3
* )
*
* Values
* (?, ?, ?),
* (?, ?, ?),
* (?, ?, ?)
*
* ON DUPLICATE KEY UPDATE
* column1=VALUES(column1),
* column2=VALUES(column2),
* column2=VALUES(column3)
*/
$query = "INSERT INTO `{$table}` ({$tableColumns}) VALUES ";
foreach ($values as $value) {
$query .= " ({$bindsPlaceholders}),";
foreach ($value as $binding) {
$bindings[] = $binding;
}
}
$query = rtrim($query, ',');
$query .= " ON DUPLICATE KEY UPDATE ";
foreach ($columns as $column) {
$query .= " {$column}=VALUES({$column}),";
}
$sql = rtrim($query, ',');
return DB::insert($sql, $bindings);
}
<?php
/**
* Useful to inject a dependency when we are not able to do it from the constructor method.
* Call like:
* . $this->injectDependency('UserRepository', 'UserInterface');
* . $this->injectDependency('UserRepository', 'UserRepository');
*
* @author Exadra37(Paulo Silva) <exadra37atgmailpointcom>
* @since 2015/03/26
* @link https://gist.github.com/Exadra37/fd8ebf60e5ffa59e803b#file-laravel-tips-md
*
* @param string $dependency - name of the dependency like in the class name declaration
* @param string $bindTo - by preference the Interface or the class name
* @return void
*/
public function injectDependency($dependency, $bindTo)
{
App::bind($bindTo, function($dependency) {
return new $dependency;
});
$property = lcfirst($dependency);
$this->{$property} = App::make($bindTo);
}
<?php
/**
* @author Exadra37 <exadra37atgmailpointcom>
* @since 2014/07/02
*/
// Illuminate\Support\Facades\Artisan;
// First parameter it is the command
// Second parameter is an array of commands arguments
Artisan::call("brainsocket:start", array('--port' => 8081));
// Inside of command class you don't need to use the facade
$this->call("brainsocket:start", array('--port' => 8081));
/**
* @author Exadra37 <exadra37atgmailpointcom>
* @since 2014/07/02
*/
public function callInternalRoute($route, $method = 'get')
{
// call a route internally
$request = Request::create($route, $method);
// We need to replace the input sot that the query string will be available in facade Input
$request->replace($request->input());
// get the normal response from your controller as if if you have call it from the browser
$response = Route::dispatch($request)->getContent();
return $response;
}