Laravel - Mongodb创建新错误

时间:2018-03-21 15:05:04

标签: php mongodb laravel eloquent moloquent

我正在使用laravel-mongodb库(https://github.com/jenssegers/laravel-mongodb

当我想创建新记录时,我收到此错误:

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

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

这是型号: Category.php

namespace App\Models;

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

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

这是我的商店方法:

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

    return back();
}

Error Printscreen

1 个答案:

答案 0 :(得分:1)

将您的$ collection更改为string,您已将其设置为数组,这就是您收到该错误的原因

$collection = 'categories';

以下是来自库github自述文件

的示例
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class User extends Eloquent {

    protected $collection = 'users_collection';

}