adeguntoro's avatar

generate slug url for existing data ?

I have 100 row data, and i forgot to add slug column to save slug url. Now after i edit my database and my laravel, i can add slug into new data to replace my id. The question is, how can i update existing data with slug / generate url from old data ? old data

title = Hei its me

my slug url from title because it don't have slug url

domain.com/artikel/hei-its-me
0 likes
3 replies
tisuchi's avatar

You can create a temporary route for generating slugs.

Route::get('generate-slug', function(){
    
    // domain.com/artikel/hei-its-me
    $needSlugs = Model::where('slug', '')->get();

    foreach($needSlugs as $slug){
        $slug->update([
            'slug' => str_slug($slug->slug)
        ]);
    }
});

You have to make sure, your model has fillable property.

BTW, if you store full slug in the table, then it should be like this-

foreach($needSlugs as $slug){
        $slug->update([
            'slug' => 'domain.com/artikel/' . str_slug($slug->slug)
        ]);
    }
shez1983's avatar

while you can create temp. routes. you can also

  1. do this straight in MySQL.. (on 2nd thoughts this will be hard as i just googled so not worth it)

  2. do it using TINKER (via ssh into server).

Please or to participate in this conversation.