1

我正在使用 Slim3 和 Eloquent 作为模型的 ORM 构建一些 API 应用程序,并且遇到了一些奇怪的问题。

保存模型时,出现内部服务器错误,根本无法捕获错误。我的模型只有两列,id pk autoincrement 和 name(字符串 255)这是我的设置:

模型:

class Version extends Illuminate\Database\Eloquent\Model
{
    protected $table = 'version';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name'];

    protected $hidden = ['id'];
}

行动:

public function create(RequestInterface $request, ResponseInterface $response)
    {
        $params = $request->getParsedBody();
        $class = $this->model; //fully namespaced class name
        $model = new $class();
        $model->fill($params);

        return $model->save() ?
            $this->success($response, "{$this->getModelName()} successfully saved.") :
            $this->error($response, $model->errors());

    }

Illuminate\Database\Eloquent\Builder我经理将问题本地化到第 1242 行中的这部分代码

if (in_array($method, $this->passthru)) {
            return call_user_func_array([$this->toBase(), $method], $parameters);
        }

但我不知道出了什么问题

编辑:

我正在使用PHP/7.0.0-dev和 Slim3.1

Eloquent PHP 7 兼容吗?

4

1 回答 1

0

您的模型是一个 Eloquent 模型,因此您可以使用以下代码创建

$version = App\Version::create(['name' => 'Your version name']);

这将删除所有不必要的功能,并保持你的代码干净。

在您的情况下,请确保 $request->getParsedBody(); 返回 Eloquent 使用它的正确格式。

于 2016-02-24T11:03:07.953 回答