Skip to content

Commit

Permalink
Merge pull request #12 from langleyfoxall/get_next_id
Browse files Browse the repository at this point in the history
`Models::getNextId` method
  • Loading branch information
jaredkove authored Aug 28, 2018
2 parents 7932752 + 205f7b1 commit 890c32a
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/LangleyFoxall/Helpers/Models.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

/**
Expand Down Expand Up @@ -98,4 +99,22 @@ public static function getColumns(Model $model)
{
return Schema::getColumnListing($model->getTable());
}

/**
* Gets the next auto-increment id for a model
*
* @param Model $model
* @return int
* @throws \Exception
*/
public static function getNextId(Model $model)
{
$statement = DB::select('show table status like \''.$model->getTable().'\'');

if (!isset($statement[0]) || !isset($statement[0]->Auto_increment)) {
throw new \Exception('Unable to retrieve next auto-increment id for this model.');
}

return (int) $statement[0]->Auto_increment;
}
}

0 comments on commit 890c32a

Please sign in to comment.