How to check only stored checkbox value in database in laravel? - php

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

Related

return checked in modal

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.

Displaying a category when editing a post in laravel

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.

Laravel form checkbox data

I have two tables; both of which have several columns. In one, I have all licenses the user can select (with checkboxes), in the other I store what licenses the user has.
I created a model to get all licenses, and model to get what licenses the user has.
Now, I can't understand how to create a view of all licences where those that the user already has are already checked - e.g when I create the form with these checkboxes, how can I check if the user already has the license.
I can get values but I cannot get the #if syntax work.
Here is my code currently:
<div class="form-group col-sm-12">
<div class="form-check form-check-inline">
#foreach($all_license as $all_licen_row)
#foreach($drive_licenses as $lisen)
#if($lisen->license_id==$all_licen_row->id)
<input class="form-check-input" type="checkbox"
name="{{$all_licen_row->license_id}}" checked>
<label class="form-check-label"
for="inlineCheckbox1">{{ $all_licen_row->class }}</label>)
#else
<input class="form-check-input" type="checkbox" name="{{$all_licen_row->license_id}}">
<label class="form-check-label"
for="inlineCheckbox1">{{ $all_licen_row->class }}</label>)
#endif
#endforeach
#if($errors->has('id'))
<span class="help-block">
<strong class="text-danger">{{ $errors->first('drive_licence') }}</strong>
</span>
#endif
#endforeach
</div>
</div>
Something like this is usually easier to handle without using an inner loop. You can check which id's should be selected before looping through $all_license by just storing the ids from drive_licenses into an array and simply check if the $all_license id exists in the array. Example:
<?php
$ids = array();
foreach($drive_licenses as $lisen) {
array_push($ids, $lisen->license_id)
}
?>
#foreach($all_license as $all_licen_row)
#if(in_array($all_licen_row->id, $ids))
<input class="form-check-input" type="checkbox" name="{{$all_licen_row->license_id}}" checked>
<label class="form-check-label" for="inlineCheckbox1">{{ $all_licen_row->class }}</label>
#else
...
#endif
#endforeach
If you want, you could also use the ternary operator (e.g., (?:)) to shorten your code some. Example:
#foreach($all_license as $all_licen_row)
<input class="form-check-input" type="checkbox" name="{{$all_licen_row->license_id}}"{{ (in_array($all_licen_row->id, $ids) ? ' checked' : '') }}>
<label class="form-check-label" for="inlineCheckbox1">{{ $all_licen_row->class }}</label>
#endforeach

Laravel 5.5 - Check checkbox if role has permission

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

How to mark check box state Laravel

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>

Categories