poojajadav / hasmanysync
This trait can be sync data for hasmany relationship.
2.0.0
2024-06-19 10:41 UTC
Requires
- php: ^8.2
- illuminate/support: ^8.0 || ^9.0 || ^10.0 || ^11.0
Requires (Dev)
- laravel/framework: ^11.0
- laravel/helpers: ^1.7
- mockery/mockery: ^1.1
- orchestra/testbench: ^6.23 || ^7.0 || ^8.0 || ^9.0
- phpunit/phpunit: ^11.1
- sempro/phpunit-pretty-print: ^1.0
This package is auto-updated.
Last update: 2024-10-19 11:17:03 UTC
README
Introduction
This package adds has-many sync relationships to Eloquent in Laravel. It's same like sync relationship.
Installation
You can install the package via composer
composer require poojajadav/hasmanysync
Usage
Hasmany sync
Hasmany-sync relation is almost identical to standard Syncing Associations except. It'll add, update and remove data from hasmany relationship. Example:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Poojajadav\Hasmanysync\Traits\HasManySync; class Post extends Model { use HasFactory; use HasManySync; protected $guarded = []; public function comments() { return $this->hasMany(Comment::class); } }
Now you can action on relationship like:
<?php $post = Post::first(); $comments = [ [ "id" => 2, "post_id" => 1, "name" => "This comment will be update" ], ['name' => 'This comment will attach'], ]; $post->comments()->sync($comments);