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.
Related
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'm facing a problem that all checkboxes are checked. I want only the checkbox which has a value in database to be checked.
I have these three tables where place_category is the pivot table
------- -------------- ----------
places place_category categories
------ -------------- ----------
id place_id id
name category_id name
eloquent relationships are set.
Here I'm trying to associate many categories for one place but I want only the categories that I added to that place to be checked in the checkbox.
...........
#foreach($categories as $category)
<div class="col-lg-3">
<div class="form-group">
<input type="checkbox" name="category[]" value="{{$category->id}}" {{isset($category) ? 'checked': '' }} >{{ $category->name }}<br>
</div>
</div>
#endforeach
Is there any problem with my condition ?
{{isset($category) ? 'checked': '' }}
The problem is that your $category will always be set this way, it will always return true.
You are probably iterating over all the categories, so you need to compare the category with for example the product or whatever you have in the view, and check if the category id is the one selected for that product.. something like this:
#foreach($categories as $category)
<div class="col-lg-3">
<div class="form-group">
<input type="checkbox" name="category[]"
value="{{$category->id}}"
#if($category->id === $model->category_id) 'checked' #endif>
{{ $category->name }}<br>
</div>
</div>
#endforeach
Now this code will check the category only if it was attached to the model that you are listing. Show more code from the controller maybe and the view to get better help if this is not sufficient for you.
-- EDIT
Based on your edit, you are using a many to many relationship there.. so this will do it:
#if($place->categories->contains($category->id)) 'checked' #endif
You can use empty()
{{ !empty($category) ? 'checked': '' }}
You can see if the Collection of Categories the Place has, $place->categories, contains the current Category being iterated.
#foreach($categories as $category)
<div class="col-lg-3">
<div class="form-group">
<input type="checkbox"
name="category[]" value="{{$category->id}}"
{{ $place->categories->contains($category) ? 'checked' : '' }}
>{{ $category->name }}<br>
</div>
</div>
#endforeach
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 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>
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>