I am working in Roles and Permission.
I have fetched all permission from permissions table. I want to show all available permission to superadmin and at the same time if he has already that some permission then checkbox should be checked ,if some are not, checkbox should not checked for them
Permisson table has field:id,name,display_name,description,timestamp
roles table:id,name,display_name,description,timestamp
permission_user table:permission_id,user_id
Here is current state :http://imgur.com/a/bfveP
Rolecontroller:
public function edit($id)
{
$role = Role::where('id', $id)->with('permissions')->first();
$permissions = Permission::all();
// dd($role);//i am getting permission of that role
return view('manage.roles.edit')->with('permissions',$permissions)- >with('role',$role);
}
edit.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Edit</div>
<div class="panel-body">
form...
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Edit</div>
<div class="panel-body">
#foreach( $permissions as $permission )
<p>
<input type="checkbox"
name="permissions[]"
id="perm_{{ $permission->id }}"
{{ in_array($permission->id, $role->permissions->pluck('id') ) ? 'checked="checked"' : '' }}
value="{{ $permission->id }}">
{{ $permission->display_name }}
</p>
#endforeach
</div>
</div>
</div>
</div>
</div>
#endsection
Change
{{$role->display_name?'checked="checked"':''}}"
To
{{$role->display_name }}" {{Auth::user()->can($permission) ?'checked="checked"':''}}>
This will give you the following code snippet
<p>
<input type="checkbox" name="" id="{{$role->display_name }}" {{Auth::user()->can($permission) ?'checked="checked"':''}}>
{{$permission->display_name}}
</p>
This will set the id to the display name and use a coalesce operator to check if the current authenticated user (or whatever user you change that to) has the role $role.
The code from #Milo should work, but my guess is it's not working because it isn't connected to users..
Instead of Auth::user()->can($permission), try to make a method on the role model that returns a Boolean if the role contains that permission parameter. Something like $role->contains($permission).
Then do something like list the id and name(careful not to put checked in them), then afterwards, I usually do something like #if($role->contains($permission)) checked="checked" #endif
First create method to check is the permission already add in role model
public function isPermissionExist($id)
{
foreach($this->permissions as $permission){
if($permission->id == $id)
return true;
}
return false;
}
next in view
#foreach( $permissions as $permission )
<p>
<input type="checkbox"
name="permissions[]"
id="perm_{{ $permission->id }}"
#if($role->isPermissionExist($permission->id)) checked #endif
value="{{ $permission->id }}">
{{ $permission->display_name }}
</p>
#endforeach
Try this in your edit.blade.php
<label class="checkbox-inline "for="perm[{{ $permission->id }}]">
<input id="perm[{{ $permission->id }}]" name="perm[{{ $permission->id }}]" type="checkbox" value="{{ $permission->id }}"
#if($role->permissions->contains($permission->id)) checked=checked #endif
> {{ $permission->name }}
</label>
Related
I have problem returning 'checked' on my modal, here's the problem
I have these tabel called role_has_permissions that have permission_id that got foreign key from permission tabel.
*note: a Role could have many permissions like this:
In controller I already success returning permissions as usual, but the problem is when I tried to match the id value from permissions tabel with role_has_permissions
In Controller I do like this..
$permissions = DB::table('permissions')->get();
$role_permissions = DB::table('role_has_permissions')->join('permissions', 'role_has_permissions.permission_id', '=', 'permissions.id')->get();
return view('role.index', compact('role', 'permissions', 'role_permissions'));
In view:
#foreach ($permissions as $i)
#foreach ($role_permissions as $item)
<div class="form-check ps-1 mt-2">
<input {{ $i->id == $item->permission_id ? 'checked' : '' }} disabled id="{{ $i->id }}" name="permissions_id[]" class="form-check-input form-disabled" type="checkbox" value="{{ $i->id }}">
<label class="form-check-label" for="{{ $i->id }}">{{ $i->name }}</label>
</div>
#endforeach
#endforeach
The result is not like what I expected. how do I have to do this properly?
multiple foreach should not use in this case for eg:
UserListPermission
UserCreatePermission
the first foreach will loop two times
Admin->UserListPermission
The inner loop will check roles two times.
$admin->UserListPermission == UserListPermission
$admin->UserListPermission == UserCreatePermission
which is no of use instead of that we can get role permissions Id array as below
$permissions = DB::table('permissions')->get();
$role_permissions = DB::table('role_has_permissions')->join('permissions', 'role_has_permissions.permission_id', '=', 'permissions.id')->pluck('permission_id')->toArray();
return view('role.index', compact('role', 'permissions', 'role_permissions'));
and on the view page:
#foreach ($permissions as $i)
#php $checked = in_array($i->id, $role_permissions) ? 'checked' : ''; #endphp
<div class="form-check ps-1 mt-2">
<input {{ $checked }} disabled id="{{ $i->id }}" name="permissions_id[]" class="form-check-input form-disabled" type="checkbox" value="{{ $i->id }}">
<label class="form-check-label" for="{{ $i->id }}">{{ $i->name }}</label>
</div>
#endforeach
Hope it will be helpful.
I am beginner in Laravel. I make my project in Laravel 8.
I have this code:
#foreach($productIngredients as $productIngredient)
#php
if($selectedProductIngredients->contains('ingredient_id', $productIngredient->id) === true){
$item = \App\Models\SelectedProductIngredient::where('product_id', $product->id)->where('ingredient_id', $productIngredient->id)->first();
$weight = $item->weight;
} else {
$weight = null;
}
#endphp
<div class="col-6">
<div class="form-check py-2">
<input id="productIngredientId-{{ $productIngredient->id }}"
class="form-check-input enableInput" style="margin-top:10px"
name="productIngredient[]" type="checkbox"
value="{{ $productIngredient->id }}"
#if($selectedProductIngredients->contains('ingredient_id', $productIngredient->id) === true) checked #endif>
<label class="form-check-label" for="flexCheckChecked">
{{ $productIngredient->name }} [{{ $productIngredient->short_name }}]
</label>
<input id="productIngredient-{{ $productIngredient->id }}" type="text"
name="productIngredient-{{ $productIngredient->id }}" maxlength="10"
class="form-control weight-input weightMask"
style="width:100px;display: inline; margin-left:20px" placeholder=""
value="{{ $weight }}">
</div>
</div>
#endforeach
It's work fine.
I would like there to be 2 records in 1 row.
So I would like to add at the beginning and close div in case of the last record
How can I do this?
Please help me.
Do you know about the Laravel collection? It can help you.
You can use chunk method, it breaks the collection into multiple, smaller collections of a given size:
#foreach($productIngredients->chunk(2) as $chunk)
<div class="row">
#foreach($chunk as $productIngredients)
<div class="col-6">
...
</div>
#endforeach
</div>
#endforeach
I am creating a blog in Laravel. I have two tables - posts and categories.
When i want to edit one post, i have a problem with categories. My post controller is sending categories to the view, i can see them while editing, but i don't know how to set the current category in the select option.
The tables are conected:
public function posts(){
return $this->hasMany('App\Post');
}
public function category(){
return $this->belongsTo('App\Category');
}
Controller:
public function edit(Post $post)
{
$categories = Category::all();
return view('admin.posts.edit', compact('post'))->withCategories($categories);
}
View:
<form action="{{ route('posts.update', ["post" => $post->id]) }}" method="post" enctype="multipart/form-data" class="form-horizontal">
#csrf
<div class="form-body">
<div class="form-group">
<label class="col-md-3 control-label">Naslov</label>
<div class="col-md-4">
<input type="text" name='title' value='{{ old("title", $post->title) }}' class="form-control">
#if($errors->has('title'))
<div class='text text-danger'>
{{ $errors->first('title') }}
</div>
#endif
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Kategorija</label>
<div class="col-md-4">
<select class="form-control" name="category_id">
#if(count($categories) > 0)
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
#endif
</select>
#if($errors->has('category_id'))
<div class='text text-danger'>
{{ $errors->first('category_id') }}
</div>
#endif
</div>
</div>
How can i make the category display the current category of the selected post in the option select like i have for title? value='{{ old("title", $post->title) }}'
To show the currently associated Category, you can set a default "selected" option based on the $post->category_id:
<option value="{{ $category->id }}" {{ old('category_id', $post->category_id) == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
This a simple ternary that sets the selected attribute of one of the options based on the old() value, or, if old() is not set, then based on the value of $post->category_id
What you are looking for is the selected property of the <option> element.
#forelse ($categories as $category)
<option value="{{ $category->id }}" {{ $category->id == old('category_id', $post->category_id) ? 'selected' : '' }}>
{{ $category->name }}
</option>
#endforelse
A minor change as well in the in you blade template to make use of the #forelse directive for less code.
You could also change your controller code a bit:
return view('admin.posts.edit', compact('post', 'categories'));
Further the laravelcollective/html package might help you a lot in creating forms.
I have the following code that works with the Form Collective package, however this isn't working right now as the package hasn't been updated for 5.5. I am also using Spatie's Laravel Permission package
The code I have is
#foreach ($permissions as $permission)
{{Form::checkbox('permissions[]', $permission->id, $role->permissions ) }}
{{Form::label($permission->name, ucfirst($permission->name)) }}<br>
#endforeach
Which I believe is just looping through the permissions and if the permission belongs to the current role check the box.
How can I achieve this without using the package?
I have currently tried
#foreach ($permissions as $permission)
<div class="checkbox">
<label>
{{ ucfirst($permission->name) }}
</label>
<input type="checkbox" name="permissions[]" value="{{ $permission->id }}">
<br>
</div>
#endforeach
But I'm not sure how to attach the checked attribute based on whether the role has a permission in the list.
Just add the checked attribute to the checkbox HTML:
<input type="checkbox" name="permissions[]" value="{{ $permission->id }}" checked>
if you need to set it upon a condition use the following code:
<input type="checkbox" name="permissions[]" value="{{ $permission->id }}" #if(/* some condition */) checked #endif>
EDIT
Since i didn't understand the question before i'll add some details.
Assuming that your Role model has a collection of attached permissions and it is stored in the attribute $role->permissions you could do
<input type="checkbox" name="permissions[]" value="{{ $permission->id }}" #if($role->permissions->contains($permission)) checked #endif>
That way you can check if your role has the permission with id $permission->id.
Try this:
#foreach ($permissions as $permission)
<div class="checkbox">
<label>
{{ ucfirst($permission->name) }}
</label>
<input type="checkbox" name="permissions[]" value="{{ $permission->id }} {{($role->permissions == $permission->id) ? 'checked' : ''}}">
<br>
</div>
#endforeach
I am trying to create an update page and i've completed most of the page, with exception to the checkbox section. For some reason that i've yet to figure out, the checkboxes are duplicated. I am using laravel.
This is the code for that particular section of the form.
<div class="form-group">
<label>Focus Area</label>
<br>
#foreach(FocusArea::all() as $focusArea)
#if(isset($project))
<div class="checkbox material checkbox-success">
<label>
#foreach($project->getIdsOfFocusAreas() as $selectedFocusArea)
#if($selectedFocusArea == $focusArea->focus_area_id)
<input type="checkbox" name="focus-area[]" value="{{ $selectedFocusArea }}" checked>
#else
<input type="checkbox" name="focus-area[]" value="{{ $selectedFocusArea }}">
#endif
#endforeach
{{ $focusArea->name }}
</label>
</div>
<br>
#endif
#endforeach
</div>
Some extra information:
The number of elements in the array generated by FocusArea::all() is 5.
The number of elements in the array generated by getIdsOfFocusAreas() is 2.
I know that it is duplicating twice because of point number 2, im just not exactly sure why it is duplicating in the first place.
Try this:
<div class="form-group">
<label>Focus Area</label>
<br>
<?php $selectedFlug = 0; ?>
#foreach(FocusArea::all() as $focusArea)
#if(isset($project))
<div class="checkbox material checkbox-success">
<label>
#foreach($project->getIdsOfFocusAreas() as $selectedFocusArea)
#if($selectedFocusArea == $focusArea->focus_area_id)
<input type="checkbox" name="focus-area[]" value="{{ $selectedFocusArea }}" {{ ($selectedFocusArea == $focusArea->focus_area_id) ? 'checked' : '' }} >
<?php $selectedFlug = 1; ?>
#break
#else
<?php $selectedFlug = 0; ?>
#endif
#endforeach
#if($selectedFlug == 0)
<input type="checkbox" name="focus-area[]" value="{{ $focusArea->focus_area_id }}">
#endif
{{ $focusArea->name }}
</label>
</div>
<br>
#endif
#endforeach
</div>