Finding nested results inside Laravel blade template
I really hope I don't get shouted down for a vague question, but I'm struggling to find a good answer to this. I'm looking for the most efficient way to check for nested matches within a Laravel blade template.
I'm cool with PHP, but still getting used to using a framework and I'm trying to get off on the right (and most efficient) foot with it.
I have an edit user page, to which my user is assigned permissions. Just like a forum, there are literally hundreds of permissions to pick from.
The edit page is shown, and underneath there is a table of possible permissions with a radio button for yes/no.
@foreach($permissionsAvailable as $permission)
<tr>
<td class="hub3Blue hub3FontSmall">{{ $permission->name }}</td>
<td class="hub3FontSmall">{{ $permission->description }}</td>
<td>
<form name="{{$permission->id}}">
<div class="form-group">
<div class="form-check form-check-inline">
<input class="form-check-input permissionCheck" type="radio" id="{{$permission->id}}"
onclick="sendPermission('{{$permission->id}}','yes')" name="hasPermission">
<label class="form-check-label" for="hasPermission">Yes</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input permissionCheck" type="radio" id="{{$permission->id}}"
onclick="sendPermission('{{$permission->id}}','no')" name="hasPermission">
<label class="form-check-label" for="hasPermission">No</label>
</div>
</div>
</form>
</td>
</tr>
@endforeach
There is a jQuery function to update the permission_user table, either adding a permission or removing it. This is fine. What I am trying to achieve is a way of getting all of the yes/no radio buttons checked, or not, but I'm struggling with a "best practice" way of doing it.
I figured the simplest way would be to include a PHP function at the bottom of the page, but this would need to query the database for every iteration to see if the permission was allocated to that user.
The other way I suppose is to bring the permissions table in, in full to the blade template, ie compact it with the other data and then compare against the array data. Speedier I assume, but still doing a lot of work. If I have 100 users, with 100 possible permission, that's [potentially] an array of 10,000 entries.
Some guidance would be really appreciated, guys.
SQL structure/relationship
Table: permission_user
+---------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| permission_id | int(10) unsigned | NO | MUL | NULL | |
| user_id | int(10) unsigned | NO | MUL | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+---------------+------------------+------+-----+---------+----------------+
So as the table of permissions is looped, it needs to check each permission_id to see if it has a row that exists with a corresponding user_id.
check if [row_exists] in permission_user WHERE permission_id = permission_id AND user_id = user_id
... is the best way I can describe it.
I'm aware I may be asking for an efficiency that just doesn't exist.
php laravel eloquent laravel-blade
add a comment |
I really hope I don't get shouted down for a vague question, but I'm struggling to find a good answer to this. I'm looking for the most efficient way to check for nested matches within a Laravel blade template.
I'm cool with PHP, but still getting used to using a framework and I'm trying to get off on the right (and most efficient) foot with it.
I have an edit user page, to which my user is assigned permissions. Just like a forum, there are literally hundreds of permissions to pick from.
The edit page is shown, and underneath there is a table of possible permissions with a radio button for yes/no.
@foreach($permissionsAvailable as $permission)
<tr>
<td class="hub3Blue hub3FontSmall">{{ $permission->name }}</td>
<td class="hub3FontSmall">{{ $permission->description }}</td>
<td>
<form name="{{$permission->id}}">
<div class="form-group">
<div class="form-check form-check-inline">
<input class="form-check-input permissionCheck" type="radio" id="{{$permission->id}}"
onclick="sendPermission('{{$permission->id}}','yes')" name="hasPermission">
<label class="form-check-label" for="hasPermission">Yes</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input permissionCheck" type="radio" id="{{$permission->id}}"
onclick="sendPermission('{{$permission->id}}','no')" name="hasPermission">
<label class="form-check-label" for="hasPermission">No</label>
</div>
</div>
</form>
</td>
</tr>
@endforeach
There is a jQuery function to update the permission_user table, either adding a permission or removing it. This is fine. What I am trying to achieve is a way of getting all of the yes/no radio buttons checked, or not, but I'm struggling with a "best practice" way of doing it.
I figured the simplest way would be to include a PHP function at the bottom of the page, but this would need to query the database for every iteration to see if the permission was allocated to that user.
The other way I suppose is to bring the permissions table in, in full to the blade template, ie compact it with the other data and then compare against the array data. Speedier I assume, but still doing a lot of work. If I have 100 users, with 100 possible permission, that's [potentially] an array of 10,000 entries.
Some guidance would be really appreciated, guys.
SQL structure/relationship
Table: permission_user
+---------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| permission_id | int(10) unsigned | NO | MUL | NULL | |
| user_id | int(10) unsigned | NO | MUL | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+---------------+------------------+------+-----+---------+----------------+
So as the table of permissions is looped, it needs to check each permission_id to see if it has a row that exists with a corresponding user_id.
check if [row_exists] in permission_user WHERE permission_id = permission_id AND user_id = user_id
... is the best way I can describe it.
I'm aware I may be asking for an efficiency that just doesn't exist.
php laravel eloquent laravel-blade
It would be nice if you post user/permissions model relations.
– Laerte
Nov 23 '18 at 20:10
My apologies. Table structure added. There may well not be a better way of doing it than simply doing a check for every loop.
– Stephen
Nov 23 '18 at 20:26
add a comment |
I really hope I don't get shouted down for a vague question, but I'm struggling to find a good answer to this. I'm looking for the most efficient way to check for nested matches within a Laravel blade template.
I'm cool with PHP, but still getting used to using a framework and I'm trying to get off on the right (and most efficient) foot with it.
I have an edit user page, to which my user is assigned permissions. Just like a forum, there are literally hundreds of permissions to pick from.
The edit page is shown, and underneath there is a table of possible permissions with a radio button for yes/no.
@foreach($permissionsAvailable as $permission)
<tr>
<td class="hub3Blue hub3FontSmall">{{ $permission->name }}</td>
<td class="hub3FontSmall">{{ $permission->description }}</td>
<td>
<form name="{{$permission->id}}">
<div class="form-group">
<div class="form-check form-check-inline">
<input class="form-check-input permissionCheck" type="radio" id="{{$permission->id}}"
onclick="sendPermission('{{$permission->id}}','yes')" name="hasPermission">
<label class="form-check-label" for="hasPermission">Yes</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input permissionCheck" type="radio" id="{{$permission->id}}"
onclick="sendPermission('{{$permission->id}}','no')" name="hasPermission">
<label class="form-check-label" for="hasPermission">No</label>
</div>
</div>
</form>
</td>
</tr>
@endforeach
There is a jQuery function to update the permission_user table, either adding a permission or removing it. This is fine. What I am trying to achieve is a way of getting all of the yes/no radio buttons checked, or not, but I'm struggling with a "best practice" way of doing it.
I figured the simplest way would be to include a PHP function at the bottom of the page, but this would need to query the database for every iteration to see if the permission was allocated to that user.
The other way I suppose is to bring the permissions table in, in full to the blade template, ie compact it with the other data and then compare against the array data. Speedier I assume, but still doing a lot of work. If I have 100 users, with 100 possible permission, that's [potentially] an array of 10,000 entries.
Some guidance would be really appreciated, guys.
SQL structure/relationship
Table: permission_user
+---------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| permission_id | int(10) unsigned | NO | MUL | NULL | |
| user_id | int(10) unsigned | NO | MUL | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+---------------+------------------+------+-----+---------+----------------+
So as the table of permissions is looped, it needs to check each permission_id to see if it has a row that exists with a corresponding user_id.
check if [row_exists] in permission_user WHERE permission_id = permission_id AND user_id = user_id
... is the best way I can describe it.
I'm aware I may be asking for an efficiency that just doesn't exist.
php laravel eloquent laravel-blade
I really hope I don't get shouted down for a vague question, but I'm struggling to find a good answer to this. I'm looking for the most efficient way to check for nested matches within a Laravel blade template.
I'm cool with PHP, but still getting used to using a framework and I'm trying to get off on the right (and most efficient) foot with it.
I have an edit user page, to which my user is assigned permissions. Just like a forum, there are literally hundreds of permissions to pick from.
The edit page is shown, and underneath there is a table of possible permissions with a radio button for yes/no.
@foreach($permissionsAvailable as $permission)
<tr>
<td class="hub3Blue hub3FontSmall">{{ $permission->name }}</td>
<td class="hub3FontSmall">{{ $permission->description }}</td>
<td>
<form name="{{$permission->id}}">
<div class="form-group">
<div class="form-check form-check-inline">
<input class="form-check-input permissionCheck" type="radio" id="{{$permission->id}}"
onclick="sendPermission('{{$permission->id}}','yes')" name="hasPermission">
<label class="form-check-label" for="hasPermission">Yes</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input permissionCheck" type="radio" id="{{$permission->id}}"
onclick="sendPermission('{{$permission->id}}','no')" name="hasPermission">
<label class="form-check-label" for="hasPermission">No</label>
</div>
</div>
</form>
</td>
</tr>
@endforeach
There is a jQuery function to update the permission_user table, either adding a permission or removing it. This is fine. What I am trying to achieve is a way of getting all of the yes/no radio buttons checked, or not, but I'm struggling with a "best practice" way of doing it.
I figured the simplest way would be to include a PHP function at the bottom of the page, but this would need to query the database for every iteration to see if the permission was allocated to that user.
The other way I suppose is to bring the permissions table in, in full to the blade template, ie compact it with the other data and then compare against the array data. Speedier I assume, but still doing a lot of work. If I have 100 users, with 100 possible permission, that's [potentially] an array of 10,000 entries.
Some guidance would be really appreciated, guys.
SQL structure/relationship
Table: permission_user
+---------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| permission_id | int(10) unsigned | NO | MUL | NULL | |
| user_id | int(10) unsigned | NO | MUL | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+---------------+------------------+------+-----+---------+----------------+
So as the table of permissions is looped, it needs to check each permission_id to see if it has a row that exists with a corresponding user_id.
check if [row_exists] in permission_user WHERE permission_id = permission_id AND user_id = user_id
... is the best way I can describe it.
I'm aware I may be asking for an efficiency that just doesn't exist.
php laravel eloquent laravel-blade
php laravel eloquent laravel-blade
edited Nov 24 '18 at 4:08
Randika Vishman
4,51723857
4,51723857
asked Nov 23 '18 at 19:53
Stephen Stephen
308112
308112
It would be nice if you post user/permissions model relations.
– Laerte
Nov 23 '18 at 20:10
My apologies. Table structure added. There may well not be a better way of doing it than simply doing a check for every loop.
– Stephen
Nov 23 '18 at 20:26
add a comment |
It would be nice if you post user/permissions model relations.
– Laerte
Nov 23 '18 at 20:10
My apologies. Table structure added. There may well not be a better way of doing it than simply doing a check for every loop.
– Stephen
Nov 23 '18 at 20:26
It would be nice if you post user/permissions model relations.
– Laerte
Nov 23 '18 at 20:10
It would be nice if you post user/permissions model relations.
– Laerte
Nov 23 '18 at 20:10
My apologies. Table structure added. There may well not be a better way of doing it than simply doing a check for every loop.
– Stephen
Nov 23 '18 at 20:26
My apologies. Table structure added. There may well not be a better way of doing it than simply doing a check for every loop.
– Stephen
Nov 23 '18 at 20:26
add a comment |
1 Answer
1
active
oldest
votes
OK, so I was doing this from the wrong end, which I think shows my novice levels of Laravel/Eloquent use. I should have been getting all of the permissions available on page load in my user controller and then passing that array to my blade template to check.
Get all of my users permissions first in my controller:
$userPermissions= AdminUsersPermissions::where('user_id', $id)->get();
return view('admin.users.edit',compact('user','permissionsAvailable','userPermissions'));
Then in my blade template:
foreach($userPermissions as $permission)
{
$hasPermissions = $permission->permission_id;
}
Then when I'm looping through the permissions I can say:
$checkedNo = "checked";
$checkedYes = "";
if(in_array({{$permission->id}}, $hasPermissions)
{
$checkedYes = "checked";
$checkedNo = "";
}
For the form entry:
<div class="form-check form-check-inline">
<input class="form-check-input permissionCheck" type="radio" id="{{$permission->id}}" onclick="sendPermission('{{$permission->id}}','yes')" name="hasPermission" <?=$checkedYes;?> >
<label class="form-check-label" for="hasPermission">Yes</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input permissionCheck" type="radio" id="{{$permission->id}}" onclick="sendPermission('{{$permission->id}}','no')" name="hasPermission" <?=$checkedNo;?> >
<label class="form-check-label" for="hasPermission">No</label>
</div>
Well that's the best I can come up with anyway :/
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452290%2ffinding-nested-results-inside-laravel-blade-template%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
OK, so I was doing this from the wrong end, which I think shows my novice levels of Laravel/Eloquent use. I should have been getting all of the permissions available on page load in my user controller and then passing that array to my blade template to check.
Get all of my users permissions first in my controller:
$userPermissions= AdminUsersPermissions::where('user_id', $id)->get();
return view('admin.users.edit',compact('user','permissionsAvailable','userPermissions'));
Then in my blade template:
foreach($userPermissions as $permission)
{
$hasPermissions = $permission->permission_id;
}
Then when I'm looping through the permissions I can say:
$checkedNo = "checked";
$checkedYes = "";
if(in_array({{$permission->id}}, $hasPermissions)
{
$checkedYes = "checked";
$checkedNo = "";
}
For the form entry:
<div class="form-check form-check-inline">
<input class="form-check-input permissionCheck" type="radio" id="{{$permission->id}}" onclick="sendPermission('{{$permission->id}}','yes')" name="hasPermission" <?=$checkedYes;?> >
<label class="form-check-label" for="hasPermission">Yes</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input permissionCheck" type="radio" id="{{$permission->id}}" onclick="sendPermission('{{$permission->id}}','no')" name="hasPermission" <?=$checkedNo;?> >
<label class="form-check-label" for="hasPermission">No</label>
</div>
Well that's the best I can come up with anyway :/
add a comment |
OK, so I was doing this from the wrong end, which I think shows my novice levels of Laravel/Eloquent use. I should have been getting all of the permissions available on page load in my user controller and then passing that array to my blade template to check.
Get all of my users permissions first in my controller:
$userPermissions= AdminUsersPermissions::where('user_id', $id)->get();
return view('admin.users.edit',compact('user','permissionsAvailable','userPermissions'));
Then in my blade template:
foreach($userPermissions as $permission)
{
$hasPermissions = $permission->permission_id;
}
Then when I'm looping through the permissions I can say:
$checkedNo = "checked";
$checkedYes = "";
if(in_array({{$permission->id}}, $hasPermissions)
{
$checkedYes = "checked";
$checkedNo = "";
}
For the form entry:
<div class="form-check form-check-inline">
<input class="form-check-input permissionCheck" type="radio" id="{{$permission->id}}" onclick="sendPermission('{{$permission->id}}','yes')" name="hasPermission" <?=$checkedYes;?> >
<label class="form-check-label" for="hasPermission">Yes</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input permissionCheck" type="radio" id="{{$permission->id}}" onclick="sendPermission('{{$permission->id}}','no')" name="hasPermission" <?=$checkedNo;?> >
<label class="form-check-label" for="hasPermission">No</label>
</div>
Well that's the best I can come up with anyway :/
add a comment |
OK, so I was doing this from the wrong end, which I think shows my novice levels of Laravel/Eloquent use. I should have been getting all of the permissions available on page load in my user controller and then passing that array to my blade template to check.
Get all of my users permissions first in my controller:
$userPermissions= AdminUsersPermissions::where('user_id', $id)->get();
return view('admin.users.edit',compact('user','permissionsAvailable','userPermissions'));
Then in my blade template:
foreach($userPermissions as $permission)
{
$hasPermissions = $permission->permission_id;
}
Then when I'm looping through the permissions I can say:
$checkedNo = "checked";
$checkedYes = "";
if(in_array({{$permission->id}}, $hasPermissions)
{
$checkedYes = "checked";
$checkedNo = "";
}
For the form entry:
<div class="form-check form-check-inline">
<input class="form-check-input permissionCheck" type="radio" id="{{$permission->id}}" onclick="sendPermission('{{$permission->id}}','yes')" name="hasPermission" <?=$checkedYes;?> >
<label class="form-check-label" for="hasPermission">Yes</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input permissionCheck" type="radio" id="{{$permission->id}}" onclick="sendPermission('{{$permission->id}}','no')" name="hasPermission" <?=$checkedNo;?> >
<label class="form-check-label" for="hasPermission">No</label>
</div>
Well that's the best I can come up with anyway :/
OK, so I was doing this from the wrong end, which I think shows my novice levels of Laravel/Eloquent use. I should have been getting all of the permissions available on page load in my user controller and then passing that array to my blade template to check.
Get all of my users permissions first in my controller:
$userPermissions= AdminUsersPermissions::where('user_id', $id)->get();
return view('admin.users.edit',compact('user','permissionsAvailable','userPermissions'));
Then in my blade template:
foreach($userPermissions as $permission)
{
$hasPermissions = $permission->permission_id;
}
Then when I'm looping through the permissions I can say:
$checkedNo = "checked";
$checkedYes = "";
if(in_array({{$permission->id}}, $hasPermissions)
{
$checkedYes = "checked";
$checkedNo = "";
}
For the form entry:
<div class="form-check form-check-inline">
<input class="form-check-input permissionCheck" type="radio" id="{{$permission->id}}" onclick="sendPermission('{{$permission->id}}','yes')" name="hasPermission" <?=$checkedYes;?> >
<label class="form-check-label" for="hasPermission">Yes</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input permissionCheck" type="radio" id="{{$permission->id}}" onclick="sendPermission('{{$permission->id}}','no')" name="hasPermission" <?=$checkedNo;?> >
<label class="form-check-label" for="hasPermission">No</label>
</div>
Well that's the best I can come up with anyway :/
answered Nov 23 '18 at 21:20
Stephen Stephen
308112
308112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452290%2ffinding-nested-results-inside-laravel-blade-template%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It would be nice if you post user/permissions model relations.
– Laerte
Nov 23 '18 at 20:10
My apologies. Table structure added. There may well not be a better way of doing it than simply doing a check for every loop.
– Stephen
Nov 23 '18 at 20:26