PHP code example of poojajadav / hasmanysync

1. Go to this page and download the library: Download poojajadav/hasmanysync 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/ */

    

poojajadav / hasmanysync example snippets




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);
    }
}



$post = Post::first();
$comments = [
  [
       "id" => 2,
       "post_id" => 1,
       "name" => "This comment will be update"
     ],
  ['name' => 'This comment will attach'],
];

$post->comments()->sync($comments);