cviebrock / eloquent-sluggable

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

Undefined index: onUpdate #435

Closed yanekyuk closed 6 years ago

yanekyuk commented 6 years ago

Please be sure you include all the relevant information in your issue so we can help you:

I'm transferring my Laravel files to Lumen and I've got this error when seeding.

$ php artisan db:seed
Seeding: CategoriesTableSeeder

In SlugService.php line 104:
Undefined index: onUpdate  

Here's my Category model:

use Cviebrock\EloquentSluggable\Sluggable;

class Category extends Model
{
    use Sluggable;

    public function sluggable() {
        return [
            'slug' => [
                'source' => 'title'
            ]
        ];
    }
}

Here's SlugService.php line 104:

protected function needsSlugging(string $attribute, array $config): bool
    {
        if (
            $config['onUpdate'] === true ||            //  <-- This is line 104
            empty($this->model->getAttributeValue($attribute))
        ) {
            return true;
        }

        if ($this->model->isDirty($attribute)) {
            return false;
        }

        return (!$this->model->exists);
    }
RomainFrancony commented 6 years ago

Hi, the default config doesn't seems to load in Lumen, follow these steps in order to make it work

That's it ! Hope it helped.

Zacktagnan-ZubiMan commented 4 years ago

Hello, I just install your dependency on the 7.0 version inside a Laravel project, on version 7.9.2 . And I have the same problem as that issue.

I have that on the require block of the "./composer.json" file:

    "require": {
        "php": "^7.2.5",
        "cviebrock/eloquent-sluggable": "^7.0",
        "fideloper/proxy": "^4.2",
        "fruitcake/laravel-cors": "^1.0",
        "guzzlehttp/guzzle": "^6.3",
        "laravel/framework": "^7.0",
        "laravel/tinker": "^2.0"
    },

I have that on the migration file:

    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();

            $table->string('title');
            $table->string('slug');
            $table->text('text');
            $table->bigInteger('user_id')->unsigned();
            $table->boolean('active')->default(0);

            $table->timestamps();

            $table->foreign('user_id')
                ->references('id')->on('users')
                ->onDelete('cascade');
        });
    }

And that code on the Post model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;

class Post extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title', 'slug', 'text', 'user_id', 'active',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [];

    use Sluggable;

    public function sluggable()
    {
        return [
            'slug' => [
                'source' => 'title'
            ]
        ];
    }

    public function user() {
        return $this->belongsTo(User::class);
    }
}

I have also a "./database/factories/PostFactory.php" to generate some registers on the "posts" table that it's call on the DatabaseSeeder.php.

I publish the configuration file as it is said on the README with the default values, for example, the "onUpdate" still like this:

'onUpdate' => false,

So, after this all configuration, I execute that:

php artisan migrate:fresh --seed

And I have the same error at the moment of the Post seeders generation:

   ErrorException 

  Undefined index: onUpdate

  at vendor/cviebrock/eloquent-sluggable/src/Services/SlugService.php:106
    102|      */
    103|     protected function needsSlugging(string $attribute, array $config): bool
    104|     {
    105|         if (
  > 106|             $config['onUpdate'] === true ||
    107|             empty($this->model->getAttributeValue($attribute))
    108|         ) {
    109|             return true;
    110|         }

      +15 vendor frames
  16  database/seeds/DatabaseSeeder.php:25
      Illuminate\Database\Eloquent\FactoryBuilder::create()

      +37 vendor frames
  54  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

After seeing what you propose to solve the problem, I edit the "./bootstrap/app.php" file and add:

$app->configure('sluggable');

So, this happens:

Fatal error: Uncaught Error: Call to undefined method Illuminate\Foundation\Application::configure() in /var/www/html/bootstrap/app.php:44 Stack trace:

0 /var/www/html/artisan(20): require_once()

1 {main}

thrown in /var/www/html/bootstrap/app.php on line 44

The line 44 is where is the $app->configure('sluggable'); on my "./bootstrap/app.php"

So, your solution doesn't solve the problem. Maybe the last Laravel version doesn't have this method right now.

Last year, on a previous version of your package (4.8) and a previous version of Laravel (5.8.10), I haven't any problem to use it. But now ... And for the actual Laravel version, I can't install the 4.8 version of Eloquent-Sluggable.

So, any other idea to solve it?

Best regards.

mohammadreza33 commented 4 years ago

Hi @Zacktagnan-ZubiMan please see https://stackoverflow.com/questions/61174574/i-am-using-cviebrock-slug-libray-and-stuck-on-this-error-undefined-index-separ/63343255#63343255

This may help you.