craftlogan/laravel-overflow

此包已被废弃,不再维护。未建议替换包。

Laravel Overflow 包允许您轻松地将溢出列添加到表单请求中。使用此包,您可以轻松地将溢出请求值存储在数据库表的 JSON 或文本列中:)

v2.1.0 2020-09-09 21:32 UTC

This package is auto-updated.

Last update: 2023-02-14 15:23:30 UTC


README

Laravel Overflow Logo

All Contributors

Latest Version on Packagist GitHub Workflow Status MIT Licensed Quality Score Total Downloads

Laravel Overflow

Laravel Overflow 包允许您轻松地将溢出列添加到表单请求中。使用此包,您可以轻松地将溢出请求值存储在数据库表的 JSON 或文本列中:)

安装

您可以通过 Composer 安装此包

composer require craftlogan/laravel-overflow

用法

创建模型

您应该创建一个将使用溢出方法的模型。

php artisan make:model TestModel -m

-m 选项可以与模型一起生成数据库迁移。

   <?php
   
   namespace App;
   
   use Illuminate\Database\Eloquent\Model;
   
   class TestModel extends Model
   {
     
       protected $guarded = [];   // you should use either $fillable or $guarded
   }

编辑迁移

在设置迁移时,您可以使用 JSON 列或文本列

    public function up()
    {
        Schema::create('test_models', function (Blueprint $table){
            $table->increments('id');
            $table->string('name');
            //$table->text('properties');  // Use this column type if you are using sqlite or a mysql version less than 5.7
            //$table->json('properties');  // If your database supports json then I would recommend using the json column
            $table->timestamps();
        });
    }

设置字段后,您应该迁移您的表。

php artisan migrate

可用宏

allWithOverflowoverflow 宏。您可以为每个宏提供负载模型和溢出字段。

allWithOverflow

此宏返回模型的全部字段作为数组

    $request->allWithOverflow(Model_Name,Overflow_Field)
    
    //Returns

    [
        "properties" => "{"key1":"key1_value","key2":"key2_value"}",
        "name" => "overflow"
    ]

overflow

此宏只返回溢出字段作为 JSON 对象

    $request->overflow(Model_Name,Overflow_Field)

    //Returns

    "{"key1":"key1_value","key2":"key2_value"}"

通过 create 方法插入记录

您可以使用 Illuminate\Http\Request 来检索 Http 请求。

public function store(Request $request)
{
    $testmodel = TestModel::create($request->allWithOverflow(new TestModel(),'properties'));
}

此外,您还可以使用您自己的 Form Request

public function store(\App\Http\Requests\TestFormRequest $request)
{
    $testmodel = TestModel::create($request->allWithOverflow(new TestModel(),'properties'));
}

通过 save 方法插入记录

使用对象属性

    public function store(Request $request)
    {
        $testmodel = new TestModel();
        $testmodel->name = $request->name;
        $testmodel->properties = $request->overflow(new TestModel(),'properties');
        $testmodel->save();
    }   

测试

composer test

变更日志

请参阅 CHANGELOG 了解最近的变化。

贡献者 ✨

感谢这些出色的人 (emoji key)


Logan H. Craft

💻 📖

Daniel

💻

Laravel News

📝

Paul Redmond

📝

Pierre Arnissolle

💻

Hayri Can BARÇIN

📖

贡献

请参阅CONTRIBUTING以获取详细信息。

本项目遵循all-contributors规范。欢迎所有类型的贡献!

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。

本软件包使用了由Laravel Package Boilerplate构建的脚手架,该脚手架由Marcel Pociot创建。