Created
July 3, 2017 13:29
-
-
Save techouse/ef61fd73cca0e392b90f73e6ef8a9e9f to your computer and use it in GitHub Desktop.
insertIgnore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
trait InsertIgnore | |
{ | |
/** | |
* @param array $attributes | |
* | |
* @return static | |
*/ | |
public static function insertIgnore(array $attributes = []) | |
{ | |
$model = new static($attributes); | |
if ($model->usesTimestamps()) { | |
$model->updateTimestamps(); | |
} | |
$attributes = $model->getAttributes(); | |
$query = $model->newBaseQueryBuilder(); | |
$processor = $query->getProcessor(); | |
$grammar = $query->getGrammar(); | |
$table = $grammar->wrapTable($model->getTable()); | |
$keyName = $model->getKeyName(); | |
$columns = $grammar->columnize(array_keys($attributes)); | |
$values = $grammar->parameterize($attributes); | |
$sql = "INSERT IGNORE INTO {$table} ({$columns}) VALUES ({$values})"; | |
$id = $processor->processInsertGetId($query, $sql, array_values($attributes)); | |
$model->setAttribute($keyName, $id); | |
return $model; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment