forked from cviebrock/eloquent-sluggable
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added command to create slugs automatically
- Loading branch information
Daniel Klasson
committed
Nov 5, 2014
1 parent
e8f0816
commit 4cdf214
Showing
1 changed file
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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), | ||
); | ||
} | ||
|
||
} |