Laravel 5 Validate many to many data

lenny.myr

When creating a new Role an administrator can select different rights he'd like to assign to that role. After saving the freshly created Role to the database, the selected rights are sync'd to the right_role table.

public function store(CreateRoleRequest $request) {
    $role = new Role(['name' => $request->get('name')]);

    $rights = [];
    foreach ($request->get('rights') as $id => $enabled) {
        if ($enabled) {
            $rights[] = $id;
        }
    }

    $role->save();
    $role->rights()->sync($rights);

    return redirect()->route('users.index');
}

But how do I validate the submitted rights against not existing values? Can I do this within my CreateRoleRequest?

Margus Pala

This can be done with custom validator. Here is example how the custom validators can be used in CreateRoleRequest

public function __construct() {
    Validator::extend("valid_rights", function($attribute, $value, $parameters) {
        $rules = [
            'right_id' => 'exists:rights,id'
        ];
        foreach ($value as $rightId) {
                $data = [
                    'right_id' => $rightId
                ];
                $validator = Validator::make($data, $rules);
                if ($validator->fails()) {
                    return false;
                }
            }
        return true;
    });
}

public function rules() {
    return [
        'containers' => 'required|valid_rights',
    ];
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel get data from many to many relation

From Dev

Laravel 5 Eloquent ORM - Many to Many through Many to Many

From Dev

How to pass many to many relations to view as array[Laravel 5]

From Dev

Laravel 5 - access specific model on many to many relationship

From Dev

save many to many relation in Laravel elequent 5?

From Dev

Laravel 5 - Many to many relations - Getting the pivot data

From Dev

Laravel - Many to Many relationship

From Dev

Laravel 5 Many to Many - Table name in singular

From Dev

Laravel 5 One-to-many relationship master and parent data CRUD

From Dev

Laravel 5: Integrity constraint violation: 1062 - Many to Many

From Dev

Laravel 5 Eloquent Many to Many 2ndary table

From Dev

Laravel 5 - Many to Many - Attach versus Save

From Dev

Empty data returning from many to many relationship laravel 5

From Dev

Laravel 5 - Dedicated Query string filtering on many-to-many Relationship

From Dev

Laravel 5 Many to Many insert Custom Pivot Cell

From Dev

Laravel - Many To Many

From Dev

Building a dynamic query of a many-to-many relationship in Laravel 5

From Dev

Laravel 5 Eloquent count many to many relationship

From Dev

Laravel Many to many relationship get specific data

From Dev

Laravel Many to Many relation

From Dev

Laravel: get Many-to-Many column data

From Dev

Laravel Removing Pivot data in many to many relationship

From Dev

Laravel 5 Validate many to many data

From Dev

save many to many relation in Laravel elequent 5?

From Dev

Laravel 5. Many-to-many relationsip. Build query

From Dev

Laravel 5 | Many to Many Relationship not working

From Dev

Many to many relationship in Laravel

From Dev

Many to Many relationship with Laravel

From Dev

Many to Many laravel

Related Related

  1. 1

    Laravel get data from many to many relation

  2. 2

    Laravel 5 Eloquent ORM - Many to Many through Many to Many

  3. 3

    How to pass many to many relations to view as array[Laravel 5]

  4. 4

    Laravel 5 - access specific model on many to many relationship

  5. 5

    save many to many relation in Laravel elequent 5?

  6. 6

    Laravel 5 - Many to many relations - Getting the pivot data

  7. 7

    Laravel - Many to Many relationship

  8. 8

    Laravel 5 Many to Many - Table name in singular

  9. 9

    Laravel 5 One-to-many relationship master and parent data CRUD

  10. 10

    Laravel 5: Integrity constraint violation: 1062 - Many to Many

  11. 11

    Laravel 5 Eloquent Many to Many 2ndary table

  12. 12

    Laravel 5 - Many to Many - Attach versus Save

  13. 13

    Empty data returning from many to many relationship laravel 5

  14. 14

    Laravel 5 - Dedicated Query string filtering on many-to-many Relationship

  15. 15

    Laravel 5 Many to Many insert Custom Pivot Cell

  16. 16

    Laravel - Many To Many

  17. 17

    Building a dynamic query of a many-to-many relationship in Laravel 5

  18. 18

    Laravel 5 Eloquent count many to many relationship

  19. 19

    Laravel Many to many relationship get specific data

  20. 20

    Laravel Many to Many relation

  21. 21

    Laravel: get Many-to-Many column data

  22. 22

    Laravel Removing Pivot data in many to many relationship

  23. 23

    Laravel 5 Validate many to many data

  24. 24

    save many to many relation in Laravel elequent 5?

  25. 25

    Laravel 5. Many-to-many relationsip. Build query

  26. 26

    Laravel 5 | Many to Many Relationship not working

  27. 27

    Many to many relationship in Laravel

  28. 28

    Many to Many relationship with Laravel

  29. 29

    Many to Many laravel

HotTag

Archive