Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ErrorException array_merge(): Argument #1 is not an array when saving a model Laravel 5.4 #389

Closed
underwood opened this issue Sep 13, 2017 · 24 comments

Comments

@underwood
Copy link

I upgraded a Laravel app from 4.2 to 5.4 that was using eloquent-sluggable. I setup a fresh Laravel app and migrated over the controllers, views, etc. I'm having an issue with eloquent-sluggable.

I followed the E-S upgrade guide and updated the Models to the latest recommendations.

I get this error when eloquent is saving a model.

[2017-09-13 22:44:28] local.ERROR: ErrorException: array_merge(): Argument #1 is not an array in /Users/underwood/code/valet/project/vendor/cviebrock/eloquent-sluggable/src/Services/SlugService.php:65

App is L5.4, PHP 7
cviebrock/eloquent-sluggable 4.2.5

@emco83
Copy link

emco83 commented Sep 14, 2017

I have the same problem. Any fix?

@underwood
Copy link
Author

It's not pretty but you can temporarily comment out that line (SlugService.php line 67) to resolve the error. I'm going to trouble shoot the issue further and will report back when I find the source of the issue/better solution.

line 67
//return array_merge($defaultConfig, $overrides);

@emco83
Copy link

emco83 commented Sep 14, 2017

I've commented that line, but now another error occurs:

at HandleExceptions->handleError(4096, 'Argument 2 passed to Cviebrock\EloquentSluggable\Services\SlugService::buildSlug() must be of the type array, null given, called in /home/userl/public_html/laravel/vendor/cviebrock/eloquent-sluggable/src/Services/SlugService.php on line 41 and defined', '/home/user/public_html/laravel/vendor/cviebrock/eloquent-sluggable/src/Services/SlugService.php', 76, array('attribute' => 'slug'))
in SlugService.php (line 76)

@cviebrock
Copy link
Owner

Did you publish the default configuration file?

@emco83
Copy link

emco83 commented Sep 14, 2017

I've published now the configuration file, but on saving the content I'm receiving this error:
array_merge(): Argument #1 is not an array
in SlugService.php (line 65)

@cviebrock
Copy link
Owner

Do you mind adding dd($defaultConfig) just before line 65 and seeing what it is?

@emco83
Copy link

emco83 commented Sep 18, 2017

I've added dd($defaultConfig); just before the line 65 and I've tried again. After clicking the submit button, it simply redirects to the all posts page without saving the content.
Just to let you know if I comment this code in my Post model:

    use Sluggable;

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

I'm saving the content successfully, but without using the slug.

@cviebrock
Copy link
Owner

So far, every report of the Argument #1 is not an array kind has to do with an unpublished or unfindable configuration file (i.e. config/sluggable.php).

You are saying that adding dd() before line 65 ... that code never gets dumped? If that's the case, then something else is going on I think. If you are using PHPStorm or some other IDE that has debugging, can you see if the code ever reaches that line?

Or, maybe easier: can you try the dev-config-fix branch of this package? I changed something in how the default configuration is loaded that probably helps.

@emco83
Copy link

emco83 commented Sep 18, 2017

I've deleted all the files related with sluggable, installed it again with composer and it's still the same...will do some research when I have time.
I've noticed that in my config folder there's no sluggable.php file.

Laravel 5.4
cviebrock/eloquent-sluggable 4.2.4

@cviebrock
Copy link
Owner

The config doesn't publish automagically. You'll need to do that with:

php artisan vendor:publish --provider="Cviebrock\EloquentSluggable\ServiceProvider"

@emco83
Copy link

emco83 commented Sep 19, 2017

I've already tried that too.
I'm sorry, but I'm receiving the same error.

@aaronpk
Copy link

aaronpk commented Sep 20, 2017

I also just got this error on a brand new install of the plugin. (Laravel 5.4) It appears there are no defaults defined in the code. The documentation says:

Optionally, publish the configuration file if you want to change any defaults:

I tried publishing the configuration file using the command given but I didn't see a new file created or any sluggable section added to any existing config files.

I had to manually copy the sluggable.php file from the package folder to my config folder for this to work.

There appear to be two problems here:

  1. If the docs say that there are defaults that will be used if there is no config file, then that should work
  2. The command to publish the config file appears to be broken, I don't know why

@emco83
Copy link

emco83 commented Sep 20, 2017

I can confirm that the publish command is not working and that's why is not generating the sluggable.php file.

@aaronpk
Thank you very much. It's working great!

Something to add: to retrieve the slug method, the controller has to have this function whereSlug.
findBySlug() or findBySlugOrFail() doesn't work in Laravel 5.4.

The documentation of this plugin should be changed.

So, the method for fetching the specific post looks like this in my case:

    public function post($slug) {

      $post = Post::whereSlug($slug)->firstOrFail(); 

      if(count($post) > 0) {
        return view('post', compact('post'));
      }
      else {
        return redirect('/');
      }
    }

@cviebrock
Copy link
Owner

I can not duplicate this on a fresh Laravel 5.4 install.

composer create-project --prefer-dist laravel/laravel:~5.4.0 test54
cd test54
composer require cviebrock/eloquent-sluggable:^4.2
nano config/app.php
php artisan vendor:publish --provider="Cviebrock\EloquentSluggable\ServiceProvider"

(The nano command was to add the service provider to config/app.php.)

After running all those lines, the default configuration is copied from the package to config/sluggable.php.

If you can submit some reproducible code I can run to debug, I'll take a look.

@aaronpk
Copy link

aaronpk commented Sep 20, 2017

That nano command is likely the solution. From the README:

Install the package via Composer:

$ composer require cviebrock/eloquent-sluggable
The package will automatically register itself.

Optionally, publish the configuration file if you want to change any defaults:

php artisan vendor:publish --provider="Cviebrock\EloquentSluggable\ServiceProvider"

Following those instructions, the configuration file is not published.

composer create-project --prefer-dist laravel/laravel:~5.4.0 test54
cd test54
composer require cviebrock/eloquent-sluggable:^4.2
php artisan vendor:publish --provider="Cviebrock\EloquentSluggable\ServiceProvider"

No configuration file is created.

After adding Cviebrock\EloquentSluggable\ServiceProvider::class to the list of service providers in config/app.php the vendor:publish command shows that it copies the file:

php artisan vendor:publish --provider="Cviebrock\EloquentSluggable\ServiceProvider"
Copied File [/vendor/cviebrock/eloquent-sluggable/resources/config/sluggable.php] To [/config/sluggable.php]
Publishing complete.

I would recommend updating the installation docs to say that adding the service provider to the config/app.php file is necessary. Additionally, publishing the config file is not really optional like the instructions say, since there are no defaults in the code that are used if the config file is missing.

@cviebrock
Copy link
Owner

The docs you read are for the master branch, which is the Laravel 5.5 version. Registering the provider isn't required for Laravel 5.5.

The docs in the Laravel 5.4 version (i.e. package version 4.2.x) say that you'll need to register the provider manually.

@cviebrock
Copy link
Owner

That said, I will update the docs for the recent version to provide a link back to the old docs, just so it's obvious. This is a bit of a drawback of github always showing the latest version of README.md by default. :)

@aaronpk
Copy link

aaronpk commented Sep 20, 2017

ah that makes sense, I didn't even think to look at another branch on github. A link to the previous version would be very helpful!

@mhrahul
Copy link

mhrahul commented Oct 24, 2017

Try to make sure sluggable.php file is there at app/config. I have faced the same as config file didn't created and I have to make it manually!
return [ 'source' => null, 'maxLength' => null, 'method' => null, 'separator' => '-', 'unique' => true, 'uniqueSuffix' => null, 'includeTrashed' => false, 'reserved' => null, 'onUpdate' => false, ];

@prabhakar46673
Copy link

prabhakar46673 commented Nov 2, 2017

I had the same problem, it got solved by 2 steps

  1. sluggable.php config file at app/config with data
    return ['source' => null, 'maxLength' => null, 'method' => null, 'separator' => '-', 'unique' => true, 'uniqueSuffix' => null, 'includeTrashed' => false, 'reserved' => null, 'onUpdate' => false, ];

  2. Execute the command, php artisan config:cache

Thanks

@monumehta
Copy link

I had the same problem,
Solution 👍
create a sluggable.php in App/config with this data

null, 'maxLength' => null, 'maxLengthKeepWords' => true, 'method' => null, 'separator' => '-', 'unique' => true, 'uniqueSuffix' => null, 'includeTrashed' => false, 'reserved' => null, 'onUpdate' => false, ]; Done

@DJaMCo
Copy link

DJaMCo commented Oct 23, 2018

I have the same problem today.

I believe it is because I have used a [space] in the array key name. I have done it this way, because it maps to an HTML -> OPTGROUP and gives me the display value wanted - is there an easy way around this?

array_key_issues

The main issue is that I want a single array for each sub-category and then merge the required parts to give different outputs in different parts - purely to keep the "app.config" file lean.

I'd be grateful of any ideas before i scrap it and make an array for each purpose (not really ideal)

app config - example

-- EDIT --

For further info,

The only actual time i encounter an error (at the moment) is in migration

homstead - migration - example

But it may exist in other places if I've made a noob error

@devrazn
Copy link

devrazn commented Nov 10, 2020

adding the following line to the providers array in config/app.php solved the issue.

Cviebrock\EloquentSluggable\ServiceProvider::class

Also without adding this line to the config/app.php file the artisan command to publish default configuration file won't work. I don't think this is an issue but just wanted to inform this to anyone who might be stuck in the same issue in the future.

Still today, I couldn't find the docs for v4.2.* which was for Laravel 5.4.*. Please update docs with links to docs for older version of the package.

@cviebrock
Copy link
Owner

@devrazn https://github.com/cviebrock/eloquent-sluggable/tree/4.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants