PHP code example of jmjl161100 / laravel-relation-updater

1. Go to this page and download the library: Download jmjl161100/laravel-relation-updater 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/ */

    

jmjl161100 / laravel-relation-updater example snippets


use Illuminate\Database\Eloquent\Model;
use Jmjl161100\RelationUpdater\Traits\HasRelationUpdater;

class Post extends Model
{
    use HasRelationUpdater;

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

$post = Post::find(1);

$result = $post->commentsUpdate([
    ['id' => 1, 'content' => 'Updated comment'], // Update
    ['content' => 'New comment']                // Create
]);

[
    'created' => Collection, // Newly created models
    'updated' => Collection, // Updated models
    'deleted' => Collection  // Models deleted due to sync
]

$post->commentsUpdate(
                        [
                            ['id' => 1, 'input_content' => 'Updated comment']
                        ], 
                        [
                            'input_content' => 'content'
                        ]
                     );