Skip to content

Instantly share code, notes, and snippets.

@nnivxix
Last active August 12, 2023 04:45
Show Gist options
  • Select an option

  • Save nnivxix/b38b0b46dcd48c769efd652b8e187c80 to your computer and use it in GitHub Desktop.

Select an option

Save nnivxix/b38b0b46dcd48c769efd652b8e187c80 to your computer and use it in GitHub Desktop.

Revisions

  1. nnivxix revised this gist Aug 12, 2023. No changes.
  2. nnivxix revised this gist Aug 12, 2023. 1 changed file with 22 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,12 @@

    ## 1.attach is insert value to pivot table relation

    when we run this code `$user->roles()->attach([3]);` that will be add/insert data to table user_role

    | role_id| user_id |
    | ------ | ----- |
    | 3 | 1 |

    ## 2.sync is add value and delete to pivot table relation

    If user_role table have
    @@ -41,3 +47,19 @@ when we try to run this code `$user->roles()->detach();` that will be remove all
    | | |


    ## 4. syncWithoutDetaching is add value without detach and duplicate when run twice

    Let say we have table with value like this

    | role_id| user_id |
    | ------ | ----- |
    | 3 | 1 |

    when we insert using code `syncWithoutDetaching` example `$user->roles()->syncWithoutDetaching([1, 2]);` will be insert value role_id `1 and 2`

    | role_id| user_id |
    | ------ | ----- |
    | 3 | 1 |
    | 1 | 1 |
    | 2 | 1 |

  3. nnivxix revised this gist Aug 12, 2023. 1 changed file with 20 additions and 2 deletions.
    22 changes: 20 additions & 2 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,8 @@
    # Attach, detach and sync many-to-many relationships in Laravel

    - attach is insert value to pivot table relation
    - sync is add value and delete to pivot table relation
    ## 1.attach is insert value to pivot table relation

    ## 2.sync is add value and delete to pivot table relation

    If user_role table have

    @@ -23,3 +24,20 @@ $user->roles()->sync([3, 2])
    | 3 | 1 |


    ## 3. detach is remove value in pivot table relation

    If we have data like this

    | role_id| user_id |
    | ------ | ----- |
    | 2 | 1 |
    | 3 | 1 |
    | 1 | 1 |

    when we try to run this code `$user->roles()->detach();` that will be remove all data, but if we add the arguments on function detach `$user->roles()->detach(1);` will remove only `role_id : 1`

    | role_id| user_id |
    | ------ | ----- |
    | | |


  4. nnivxix created this gist Aug 12, 2023.
    25 changes: 25 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    # Attach, detach and sync many-to-many relationships in Laravel

    - attach is insert value to pivot table relation
    - sync is add value and delete to pivot table relation

    If user_role table have

    | role_id| user_id |
    | ------ | ----- |
    | 1 | 1 |
    | 2 | 1 |

    ```php

    // run this syntax
    $user->roles()->sync([3, 2])

    ```

    | role_id| user_id |
    | ------ | ----- |
    | 2 | 1 |
    | 3 | 1 |


    46 changes: 46 additions & 0 deletions testTableRelation.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    <?php

    namespace App\Console\Commands;

    use App\Models\User;
    use Illuminate\Console\Command;

    class TestRelation extends Command
    {
    protected $signature = 'rel:db';

    protected $description = 'Command description';

    public function handle()
    {
    // $this->attach();
    $this->sync();
    // $this->detach();
    // $this->syncWithoutDetaching();
    }

    public function attach()
    {
    $user = User::find(1);

    $user->roles()->attach([ 2, 3]);
    }
    public function sync()
    {
    $user = User::find(1);

    $user->roles()->sync([3, 2]);
    }
    public function detach()
    {
    $user = User::find(1);

    $user->roles()->detach();
    }
    public function syncWithoutDetaching()
    {
    $user = User::find(1);

    $user->roles()->syncWithoutDetaching([1, 2, 3]);
    }
    }