Skip to content

Commit

Permalink
added command to create slugs automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Klasson committed Nov 5, 2014
1 parent e8f0816 commit 4cdf214
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions src/Cviebrock/EloquentSluggable/CreateSlugsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class CreateSlugsCommand extends Command {

/**
* The console command name.
*
* @var string
*/
protected $name = 'slugs:create';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create slugs for Model.';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$model_name = ucfirst($this->argument('model'));

$model = new $model_name;

try
{
$model = new $model_name;
}
catch(Exception $e)
{
$this->error('The model was not found');
}
$result = $model::all();

foreach($result as $item) {

if($this->option('force'))
{
$item->resluggify();
}
else
{
$item->sluggify();
}

$item->save();
}
}

/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
array('model', InputArgument::REQUIRED, 'The model you want to update the slugs for.'),
);
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
array('force', null, InputOption::VALUE_NONE, 'Overwrite existing slugs.', null),
);
}

}

0 comments on commit 4cdf214

Please sign in to comment.