phpmongodblaraveleloquentmoloquent

Laravel - Mongodb Create new error


I'm using the laravel-mongodb library ( https://github.com/jenssegers/laravel-mongodb )

When I want to create a new record, i get this error:

strlen() expects parameter 1 to be string, array given

in project.local/vendor/mongodb/mongodb/src/Collection.php:104

This is the Model: Category.php

namespace App\Models;

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class Category extends Eloquent
{
    protected $collection = [ 'categories' ];
    protected $fillable = [ 'name', 'parentId' ];
}

And this is my store method:

public function store(Request $request)
{
    // e1: Category::create([ 'name' => 'T-shirts' ]);
    $n = new Category;
    $n->name = 'T-shirts';
    $n->save();

    return back();
}

Error Printscreen


Solution

  • Change your $collection to string, you have set it as array which is why you are getting that error

    $collection = 'categories';
    

    Here is an example from that library github Readme

    use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
    
    class User extends Eloquent {
    
        protected $collection = 'users_collection';
    
    }