php - Laravel 静态创建方法

标签 php laravel

我从 5.1 开始使用 laravel,我一直在使用 Model::create 方法来创建新的数据库记录。目前,我需要深入研究 laravel API 以找出该方法的返回类型(因为我忘记了这一点,而且自从我上次使用 Laravel 框架以来事情可能会发生变化)。

所以,当我试图找到 static create(..) 方法时,我发现自 5.3 版本以来,该方法的 api 文档丢失了。

该方法缺少 api 文档的原因是什么?使用静态 create 方法是否仍然安全,或者它会被弃用?

此外,laravel 文档缺少有关嵌套 Controller 的信息,该信息位于 laravel 5.25.3 文档中...

最佳答案

来自upgrade docs for 5.4

The create & forceCreate Methods

The Model::create & Model::forceCreate methods have been moved to the Illuminate\Database\Eloquent\Builder class in order to provide better support for creating models on multiple connections. However, if you are extending these methods in your own models, you will need to modify your implementation to call the create method on the builder. For example:

public static function create(array $attributes = [])
{
    $model = static::query()->create($attributes);

    // ...

    return $model;
}

关于php - Laravel 静态创建方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46158446/

相关文章:

php - 在 mySQL 中查询,以随机顺序从每个 'purpose' 11 中选择 44 个问题 PHP/Laravel

Laravel cursorPagination 返回混合了数组和对象的结果

php - Laravel 5 - 接口(interface)不可实例化

php - 使用 Zend_Http_Client 时出现内容类型错误

php - mysqli_fetch_assoc()需要参数/调用成员函数bind_param()错误。如何获取并修复实际的mysql错误?

php - PHP 表单,首次输入后从数据库获取数据

laravel - 在 eloquent 中执行子查询?

php - 在 Laravel 一对多关系上插入数据失败

php - 如何在Sonata Admin的Show Action中显示图像?

phpDoc PDF模板