Check checkboxes when choosing value from a DB in PHP (Laravel)

0

I have a problem that I do not know how to solve and I do not know how to search. I'm building a system in php - laravel . This system will have user levels.

I've done all of the sign-ups, changes, user views, groups, and permissions. However, I caught on a part that probably will have to use javascript, but I have very basic knowledge in JS.

At the time of adding a user, I have a SELECT with the groups (eg ADMIN, USERS ...) and a checkbox list with the general permissions (of all groups) ... until then I can select the group and what assumptions I want.

But I needed to do the following: That when I select (eg ADMIN) in the select, JS already filled in all the permissions that ADMIN has right, and then the user could manually select whether they want any more permissions or not.

I need to do this because when I add the user, I need to know which permissions are right for each group, so I know which one to register.

    
asked by anonymous 29.04.2016 / 17:11

2 answers

0

I would recommend that you make an AJAX request to fetch a JSON with the default permissions.

For example, you can create a route in Laravel ('/ permissions / {group}') that you can check the group and return JSON, I do not know how you saved it in BD, return that LAravel mounts the JSON.

In your script, when the date comes with the response of this AJAX, you run in a forEach this JSON, for each default entry, your DOM search using jQuery and arrow this checkbox as checked.

An event in select for when the value changes, it fetches the default permissions by AJAX and it's done.

If you want to make hardcoded still and ignore AJAX, you can put these JSON with the permissions of each group in an array in your own script, it will avoid requests to fetch JSON but it is a less elegant solution from my point of view .

    
29.04.2016 / 17:30
0

First, thank you for listening, Evandro. Come on... I have never used Json, and as I said I know JS's basics well. But I really saw the logic of what you said, and I looked into those subjects.

See if I'm correct: - Should I create a JSON with the bank settings: $ group-> permissions (array) (in PHP) - This through a route in the laravel - Call this JSON in view via AJAX - Make a function in the select to 'check' the permissions when the group is selected. Ok?

Now other questions have arisen, if possible, I count on your help: 1st: when generating this JSON, do I need to generate a JSON file in some directory? or is it possible to generate a JSON variable and play it for view, and get access to it with JS? --- > I saw the command, eg. $ groupjson = $ group-> permissions-> to_json (); to transform my array (object) into JSON.

After your answers we continue the challenge.

Again, thank you very much for your attention. I did not know much about this JSON, but from what I researched, you can do many useful things with it, especially the relationship with other languages.

Hugs

    
30.04.2016 / 15:19