Code Monkey home page Code Monkey logo

Comments (4)

cviebrock avatar cviebrock commented on May 18, 2024 1

This probably doesn't work because, when you create a new Model, the ID field is null until it's saved.

I bet it works if you re-slug an existing model (i.e. one where the ID already exists).

However, in your case, I would just slug from the title and handle everything else in your routing. Something like:

Route::get('/posts/{id}-{slug}', 'MyController@show')
    ->where('id', '\d+');

Make sense?

from eloquent-sluggable.

jasonvarga avatar jasonvarga commented on May 18, 2024

Excuse the delay. Finally got back on this project, tried it out and this solution works perfectly.
Thanks for your help.

from eloquent-sluggable.

chicag0 avatar chicag0 commented on May 18, 2024

Hi @cviebrock
I'm interested in doing something similar, but I would rather keep the id and slug within the same piece of the url. In my case I am encoding the model ID to obfuscate the post number. So 'blog/1' would become 'blog/xRgre-Title-of-blog-post'.

As you said, at the time the model is saved there is not yet an ID, so that part is ignored, but if I resluggify it works great. This is what I am currently doing:

$blog->save();
$blog->resluggify();
$blog->save();

My question is, can I do something within Slugify or within my Model to do the second save automatically? I would to just $blog->save() throughout my controllers have something else handle the rest. Maybe using events or binding?

from eloquent-sluggable.

Devbymitch avatar Devbymitch commented on May 18, 2024

For posterity, here's how I solved this issue. Keep in mind using this will fire off the model saved Event with the slug still null or its previous value. If that doesn't work for your use case you'd just have to add some more logic into the saving method somewhere.

SluggableExtended

trait SluggableExtended
{
    use Sluggable {
    }

    // Overwrite this method
    public static function bootSluggable()
    {
        static::observe(app(SluggableExtendedObserver::class));
    }

    // Added for re-saving after slugged without firing saved event
    public function saveWithoutEvents(array $options=[])
    {
        return static::withoutEvents(function() use ($options) {
            return $this->save($options);
        });
    }
}

SluggableExtendedObserver

class SluggableExtendedObserver extends SluggableObserver
{
    public function saving(Model $model)
    {
        //return $this->generateSlug($model, 'saving');
    }

    public function saved(Model $model)
    {
        $generateReturn = $this->generateSlug($model, 'saved');
        $model->saveWithoutEvents();
        return $generateReturn;
    }
}

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.