Code Monkey home page Code Monkey logo

Comments (14)

adriandmitroca avatar adriandmitroca commented on May 20, 2024 12

I've found a much quicker way to achieve this basically with no coding at all.

config(['sluggable.onUpdate' => true]);
Model::all()->each->save();

from eloquent-sluggable.

dan-klasson avatar dan-klasson commented on May 20, 2024

I created a pull request for this: #77

from eloquent-sluggable.

hamstu avatar hamstu commented on May 20, 2024

👍

Works great, A++, would merge. :)

from eloquent-sluggable.

MarcosBL avatar MarcosBL commented on May 20, 2024

Please do ! just found the need myself :) 👍

from eloquent-sluggable.

cviebrock avatar cviebrock commented on May 20, 2024

Sorry I never responded to this sooner. If you still need this, and the master branch can't do it for you, please submit an MR again and I'll add it.

from eloquent-sluggable.

 avatar commented on May 20, 2024

I found that this problem has been solved. What command should I use in order to create slugs for preexisting records?

from eloquent-sluggable.

dan-klasson avatar dan-klasson commented on May 20, 2024

First you need to copy this file over:

dan-klasson@4cdf214

Then to run you do slugs:create ModelName. You can also add in the --force flag if you want to overwrite existing slugs.

Was planning to create a new pull request for this but never got around to it.

from eloquent-sluggable.

 avatar commented on May 20, 2024

@dan-klasson

Thank you very much. In laravel 5, in order to run the command, you have to write the model prefixed by its namespace
php artisan slugs:create App\Course

from eloquent-sluggable.

kenmoini avatar kenmoini commented on May 20, 2024

Has this been merged into the master branch? It'd be rather handy to batch create the slugs for existing table rows and a seemingly important feature

from eloquent-sluggable.

dan-klasson avatar dan-klasson commented on May 20, 2024

@kenmoini: I don't think it has. I haven't used Laravel in ages. At work now but I'll try to clean up the code a bit and submit a new pull request when I get home.

from eloquent-sluggable.

dan-klasson avatar dan-klasson commented on May 20, 2024

Another pull request created. Please merge: #208

from eloquent-sluggable.

vvictorov avatar vvictorov commented on May 20, 2024

Working with Laravel 5.5. You have to place it in App\Console and include it in the $commands array in Kernel.php

<?php namespace App\Console;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use \Cviebrock\EloquentSluggable\Services\SlugService;

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 handle()
	{
		$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) {
			$field = $item->sluggable()['slug']['source'];
			$slug = SlugService::createSlug($model, 'slug', $item->$field);
			$item->slug = $slug;
			$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),
		);
	}

}

from eloquent-sluggable.

aurawindsurfing avatar aurawindsurfing commented on May 20, 2024

I use migrations to do the same thing, it is somehow cleaner and also does not pollute your commands since you will most likely run it only once to catch up with missing slugs for a model.

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\ConsoleOutput;
use App\Classified;

class GenerateSlugs extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $classifieds = Classified::all();

        $output = new ConsoleOutput();
        error_log('Fixing missing slugs');
        $bar = new ProgressBar($output, count($classifieds));

        foreach ($classifieds as $classified) {

            $bar->advance();
            $classified->save();

        }

        $bar->finish();
        error_log("\n");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

from eloquent-sluggable.

dapseen avatar dapseen commented on May 20, 2024

This feature is hidden, is not in the doc

from eloquent-sluggable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.