The Laravel Overflow package will allow adding an overflow column to a form request easily. Use this package to make it easy to store overflow request values in a JSON or Text column on a database table:)
You can install the package via composer:
composer require craftlogan/laravel-overflow
You should create a model which will use the overflow method.
php artisan make:model TestModel -m
-m option generates database migration with the model.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class TestModel extends Model
{
protected $guarded = []; // you should use either $fillable or $guarded
}
When setting up a migration you can use a json column or a text column:
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();
});
}
After setting up fields you should migrate your table.
php artisan migrate
There are allWithOverflow
and overflow
macros.You can payload Model and Overflow Field for each macros.
This macro returns all fields of model as an array
$request->allWithOverflow(Model_Name,Overflow_Field)
//Returns
[
"properties" => "{"key1":"key1_value","key2":"key2_value"}",
"name" => "overflow"
]
This macro returns only Overflow Field as a json object
$request->overflow(Model_Name,Overflow_Field)
//Returns
"{"key1":"key1_value","key2":"key2_value"}"
You can use Illuminate\Http\Request
for retrieving Http Requests.
public function store(Request $request)
{
$testmodel = TestModel::create($request->allWithOverflow(new TestModel(),'properties'));
}
Also, you can use your custom Form Request
.
public function store(\App\Http\Requests\TestFormRequest $request)
{
$testmodel = TestModel::create($request->allWithOverflow(new TestModel(),'properties'));
}
Using with the object Attributes:
public function store(Request $request)
{
$testmodel = new TestModel();
$testmodel->name = $request->name;
$testmodel->properties = $request->overflow(new TestModel(),'properties');
$testmodel->save();
}
composer test
Please see CHANGELOG for more information what has changed recently.
Thanks goes to these wonderful people (emoji key):
Logan H. Craft 💻 📖 |
Daniel 💻 |
Laravel News 📝 |
Paul Redmond 📝 |
Pierre Arnissolle 💻 |
Hayri Can BARÇIN 📖 |
Please see CONTRIBUTING for details.
This project follows the all-contributors specification. Contributions of any kind welcome!
The MIT License (MIT). Please see License File for more information.
This package used scafolding from the Laravel Package Boilerplate built by Marcel Pociot