cviebrock / eloquent-sluggable

Easy creation of slugs for your Eloquent models in Laravel
MIT License
3.9k stars 458 forks source link

How to properly use reserve slugs using a closure functions on the config? #372

Closed codeitlikemiley closed 7 years ago

codeitlikemiley commented 7 years ago

I tried Using this

'reserved' => function(Model $model) {
        return $model->forbiddenSlug();
    },

so for example in my App\User.php

i added

public function forbiddenSlug()
    {
        return 
        ['about', 'abuse', 'account', 'accounts', 'admin', 'admins', 'administrator',
        'administrators', 'anonymous', 'assets', 'billing', 'billings', 'board', 'calendar',
        'contact', 'copyright', 'e-mail', 'email', 'example', 'feedback', 'forum',
        'hostmaster', 'image', 'images', 'inbox', 'index', 'invite', 'jabber', 'legal',
        'launchpad', 'manage', 'media', 'messages', 'mobile', 'official', 'payment',
        'picture', 'pictures', 'policy', 'portal', 'postmaster', 'press', 'privacy',
        'private', 'profile', 'search', 'sitemap', 'staff', 'stage', 'staging', 'static',
        'stats', 'status', 'support', 'teams', 'username', 'usernames', 'users', 'webmail',
        'webmaster', 'login', 'register', 'dashboard', 'backoffice', 'use', 'jars', 'main',
        'data', 'api', 'www', 'test', 'heldesk', 'superadmin', 'gallery', 'employee',
        'employees', 'client', 'clients', 'client-portal', 'spam','public', 'free', 'coupon',
        'coupons', 'discount', 'discounts', 'paid', 'thankyou', 'thank-you', 'blog'];
    }

but im getting this error when i try to create a new user

TypeError: Argument 1 passed to Illuminate\Foundation\Bootstrap\LoadConfiguration::{closure}() must be an instance of Model, instance of App\User given, called in /var/www/sparky/vendor/cviebrock/eloquent-sluggable/src/Services/SlugService.php on line 215

So i tried

<?php

use App\User;

return [
.....
    'reserved' => function(User $user) {
        return $user->forbiddenSlug();
    },

It Worked , But i want the model be dynamic since Some Models Dont Need to Required a Forbidden Slug so i just return null...

How can i Can Pass a Dynamic Model in this Closure...

Hope Someone can help me thanks

codeitlikemiley commented 7 years ago

Ok got it working by using

<?php

use Illuminate\Database\Eloquent\Model;

return [
....

    'reserved' => function(Model $model) {
        return $model->forbiddenSlug();
    },
cviebrock commented 7 years ago

My guess is it's a namespacing issue. You'll either need to use 'Illuminate/Database/Eloquent/Model' in your closure signature, or import it in your class.

I'm also assuming that you User model extends the Eloquent Model.

On Jun 22, 2017, at 7:36 AM, Code It Like Miley notifications@github.com wrote:

I tried Using this

'reserved' => function(Model $model) { return $model->forbiddenSlug(); }, so for example in my App\User.php

i added

public function forbiddenSlug() { return ['about', 'abuse', 'account', 'accounts', 'admin', 'admins', 'administrator', 'administrators', 'anonymous', 'assets', 'billing', 'billings', 'board', 'calendar', 'contact', 'copyright', 'e-mail', 'email', 'example', 'feedback', 'forum', 'hostmaster', 'image', 'images', 'inbox', 'index', 'invite', 'jabber', 'legal', 'launchpad', 'manage', 'media', 'messages', 'mobile', 'official', 'payment', 'picture', 'pictures', 'policy', 'portal', 'postmaster', 'press', 'privacy', 'private', 'profile', 'search', 'sitemap', 'staff', 'stage', 'staging', 'static', 'stats', 'status', 'support', 'teams', 'username', 'usernames', 'users', 'webmail', 'webmaster', 'login', 'register', 'dashboard', 'backoffice', 'use', 'jars', 'main', 'data', 'api', 'www', 'test', 'heldesk', 'superadmin', 'gallery', 'employee', 'employees', 'client', 'clients', 'client-portal', 'spam','public', 'free', 'coupon', 'coupons', 'discount', 'discounts', 'paid', 'thankyou', 'thank-you', 'blog']; }

but im getting this error when i try to create a new user

TypeError: Argument 1 passed to Illuminate\Foundation\Bootstrap\LoadConfiguration::{closure}() must be an instance of Model, instance of App\User given, called in /var/www/sparky/vendor/cviebrock/eloquent-sluggable/src/Services/SlugService.php on line 215 So i tried

<?php

use App\User;

return [ ..... 'reserved' => function(User $user) { return $user->forbiddenSlug(); },

It Worked , But i want the model be dynamic since Some Models Dont Need to Required a Forbidden Slug so i just return null...

How can i Can Pass a Dynamic Model in this Closure...

Hope Someone can help me thanks

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

cviebrock commented 7 years ago

Yeah, that was it. :)

murph133 commented 7 years ago

When using multiple models, it broke. It seems like the solution above is designed for a single model.

Solution for multiple models:

\config\sluggable.php

use Illuminate\Database\Eloquent\Model;
use \App\Account;

'reserved' => function(Model $model){
     return Account::reservedSlugs();
},

\App\Account:

public static function reservedSlugs(){
     $locations = array('england', 'scotland', 'northern-ireland', 'Wales');
     $oldAccountSlugs = AccountSlug::pluck('slug')->toArray();
     return array_merge($locations, $oldAccountSlugs);
}

In the example above, I was using onUpdate = true, so I stored the old slugs in a separate database table. I do not want those slugs to be reused because I am using them for redirects to the current slug.