Created
July 3, 2017 13:29
-
-
Save techouse/ef61fd73cca0e392b90f73e6ef8a9e9f to your computer and use it in GitHub Desktop.
Revisions
-
techouse created this gist
Jul 3, 2017 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,39 @@ <?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; } }