Skip to content

Instantly share code, notes, and snippets.

@httpspace
Last active August 29, 2015 14:08
Show Gist options
  • Save httpspace/cc3a009ee8dc849693be to your computer and use it in GitHub Desktop.
Save httpspace/cc3a009ee8dc849693be to your computer and use it in GitHub Desktop.
群組管理laravel+Sentry2+x-editable+select2
public function update(){
if(Input::has('name')){
$user = Sentry::findUserById(Input::get('pk'));
$userGroups = $user->groups()->lists('group_id');
$selectedGroups = Input::get('value');
$groupsToAdd = array_diff($selectedGroups, $userGroups);
$groupsToRemove = array_diff($userGroups, $selectedGroups);
// Assign the user to groups
foreach ($groupsToAdd as $groupId)
{
$group = Sentry::getGroupProvider()->findById($groupId);
$user->addGroup($group);
}
// Remove the user from groups
foreach ($groupsToRemove as $groupId)
{
$group = Sentry::getGroupProvider()->findById($groupId);
$user->removeGroup($group);
}
}
}
View::composer('head', function($view)
{
//x-editable
$css[] ='//cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/bootstrap3-editable/css/bootstrap-editable.css';
$js[] ='//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js';
$js[] ='//cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/bootstrap3-editable/js/bootstrap-editable.min.js';
//select2
$css[] ='//cdnjs.cloudflare.com/ajax/libs/select2/3.5.1/select2-bootstrap.css';
$css[] ='//cdnjs.cloudflare.com/ajax/libs/select2/3.5.1/select2.min.css';
$js[] ='//cdnjs.cloudflare.com/ajax/libs/select2/3.5.1/select2.js';
$view->with('js',$js)->with('css',$css);
});
$(function(){
$('.user_group').editable({
url: '/Group/update',
source: [
{id: 1, text: '一般會員'},
{id: 2, text: '管理員'},
{id: 3, text: '工讀生'},
],
select2: {
width: 200,
multiple: true,
allowClear: false
},
success: function(response, newValue) {
// console.log(response);
$('.debug').html(response);
}
});
});
<a href="#" class="user_group" data-type="select2"
data-value="{{implode(',', User::find(id)->groups->lists('group_id'))}}"
data-name="user_group" data-pk="id" data-title="選擇群組"></a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment