1. Go to this page and download the library: Download czim/laravel-nestedupdater library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
class YourModel extends Model
{
use \Czim\NestedModelUpdater\Traits\NestedUpdatable;
// ...
class YourCustomizedModel extends Model
{
use \Czim\NestedModelUpdater\Traits\NestedUpdatable;
/**
* You can refer to any class, as long as it implements the
* \Czim\NestedModelUpdater\Contracts\ModelUpdaterInterface.
*
* @var class-string<\Czim\NestedModelUpdater\Contracts\ModelUpdaterInterface>
*/
protected $modelUpdaterClass = \Your\UpdaterClass\Here::class;
/**
* Additionally, optionally, you can set a class to be used
* for the configuration, if you need to override how relation
* configuration is determined.
*
* This class must implement
* \Czim\NestedModelUpdater\Contracts\NestingConfigurationInterface
*
* @var class-string<\Czim\NestedModelUpdater\Contracts\NestingConfigurationInterface>
*/
protected $modelUpdaterConfigClass = \Your\UpdaterConfigClass::class;
// Instantiate the modelupdater
$updater = new \Czim\NestedModelUpdater\ModelUpdater(YourModel::class);
// Or by using the service container binding (won't work in Laravel 5.4)
$updater = app(\Czim\NestedModelUpdater\Contracts\ModelUpdaterInterface::class, [ YourModel::class ]);
// Perform a nested data create operation
$model = $updater->create([ 'some' => 'create', 'data' => 'here' ]);
// Perform a nested data update on an existing model
$updater->update([ 'some' => 'update', 'data' => 'here' ], $model);
'relations' => [
// The model class:
App\Models\Post::class => [
// the data nested relation attribute
// with a value of true to allow updates with default settings
'comments' => true
],
App\Models\Comment::class => [
// this time, the defaults are overruled to only allow linking,
// not direct updates of authors through nesting
'author' => [
'link-only' => true
]
]
],
// Instantiate the modelupdater
$updater = new \Czim\NestedModelUpdater\ModelUpdater(YourModel::class);
// Perform a nested data create operation, disregarding any fillable guard
$model = $updater->forceCreate([ 'user_id' => 1, 'some' => 'create', 'data' => 'here' ]);
// Instantiate the modelupdater
$updater = new \Czim\NestedModelUpdater\ModelUpdater(YourModel::class);
// Queue an non-fillable attribute to be stored on the create model
$updater->setUnguardedAttribute('user_id', 1);
// Perform a nested data create operation
$model = $updater->create([ 'some' => 'create', 'data' => 'here' ]);