Checkbox Duplication - php

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>

Related

Laravel HTML form issue

<form action="/group/{id}/save/group" method="POST">
#csrf
<table id="modal-table">
#foreach($user_list->get() as $f)
<tr>
<td>
<div class="image">
#if($f->follower->avatar_location)
<img src="{{asset('storage/'.$f->follower->avatar_location)}}" class="img-circle" style="max-height:50px;" />
#else
<img src="{{url('/')}}/assets/media/icons/socialbuttons/user.png" class="img-circle" style="max-height:50px;"/>
#endif
</div>
<div class="detail">
#if($f->follower->verified == 1)
{{ $f->follower->name }}<img id="verified_img_sm_mess_list" src="{{url('/')}}/assets/media/icons/socialbuttons/octagonal_star.png" alt="Recensioni">
#else
{{ $f->follower->name }}</h3>
#endif
<small id="mess_list_uname">{{ '#'.$f->follower->username }}</small>
</div>
<input name="group" value="{{ $group->hobby->id }}">
<input name="person" value="{{ $f->follower->id }}">
<button type="submit" class="btn-link">Go</button>
</td>
</tr>
#endforeach
</table>
</form>
Hey a small problem! in the Foreach loop and every member in the list, I tried some bare-bones to get each users id and hobby id (to add to database, but that's not important). For some reason, when I take the input 'name' and click the Go button, All "id's" of everyone in the list are executed, Not the individual id of each user. Meaning.. each user displays their individual id and a go button, but click go and all id's are collected, not just one. It's probably really obvious but any ideas why this is happening?? Thanks!
Like said Tim Lewis in the comment, you need to put your form inside your foreach, so when the "Go" button is clicked, only the corresponding form will send the data, like this :
<table id="modal-table">
#foreach($user_list->get() as $f)
<form action="/group/{id}/save/group" method="POST">
#csrf
<tr>
<td>
<div class="image">
#if($f->follower->avatar_location)
<img src="{{asset('storage/'.$f->follower->avatar_location)}}" class="img-circle" style="max-height:50px;" />
#else
<img src="{{url('/')}}/assets/media/icons/socialbuttons/user.png" class="img-circle" style="max-height:50px;"/>
#endif
</div>
<div class="detail">
#if($f->follower->verified == 1)
{{ $f->follower->name }}<img id="verified_img_sm_mess_list" src="{{url('/')}}/assets/media/icons/socialbuttons/octagonal_star.png" alt="Recensioni">
#else
{{ $f->follower->name }}</h3>
#endif
<small id="mess_list_uname">{{ '#'.$f->follower->username }}</small>
</div>
<input name="group" value="{{ $group->hobby->id }}">
<input name="person" value="{{ $f->follower->id }}">
<button type="submit" class="btn-link">Go</button>
</td>
</tr>
</form>
#endforeach
</table>
<table id="modal-table">
#foreach($user_list->get() as $f)
<form action="/group/{id}/save/group" method="POST">
#csrf
<tr>
<td>
<div class="image">
#if($f->follower->avatar_location)
<img src="{{asset('storage/'.$f->follower->avatar_location)}}" class="img-circle" style="max-height:50px;" />
#else
<img src="{{url('/')}}/assets/media/icons/socialbuttons/user.png" class="img-circle" style="max-height:50px;"/>
#endif
</div>
<div class="detail">
#if($f->follower->verified == 1)
{{ $f->follower->name }}<img id="verified_img_sm_mess_list" src="{{url('/')}}/assets/media/icons/socialbuttons/octagonal_star.png" alt="Recensioni">
#else
{{ $f->follower->name }}</h3>
#endif
<small id="mess_list_uname">{{ '#'.$f->follower->username }}</small>
</div>
<input name="group" value="{{ $group->hobby->id }}">
<input name="person" value="{{ $f->follower->id }}">
<button type="submit" class="btn-link">Go</button>
</td>
</tr>
</form>
#endforeach
</table>
check this part of Jérôme W post
<form action="/group/{id}/save/group" method="POST">
Change to this
<form action="/group/{{$group->id}}/save/group" method="POST">

Adding a div row for a new row in Laravel

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

How to get if an value is inside of an array in a request in Laravel 6 blade view?

I have a form where you can check filters which will later filter a search query. The form is working but I would love to mark the checked checkboxes after the request as checked that the user can see what he selected.
It's working with one checkbox but if I add an array it's not working anymore.
<input type="text" class="form-control bg-transparent border-0 searchbox" placeholder="{{ __('general.search_placeholder') }}" value="{{ app('request')->input('q') }}" name="q" aria-label="Search" aria-describedby="button-addon2">
<div class="input-group-append">
<button class="btn border-0 searchbox" type="submit " id="button-addon2"><i class="fas fa-search"></i></button>
</div>
#foreach($collections as $collection)
<div class="form-check">
<input class="form-check-input" type="checkbox" value="{{$collection->id}}" name="s[]" id="defaultCheck{{$collection->id}}" {{ app('request')->input('s') == $collection->id ? 'checked' : '' }}>
<label class="form-check-label" for="defaultCheck{{$collection->id}}">
{{$collection->id}}
</label>
</div>
#endforeach
</form>```
Does anybody know how to solve that?
If s[] is an array of checked checkboxes, you could use in_array to test if the current $collection->id is in there:
{{ in_array($collection->id, app('request')->input('s')) ? 'checked' : '' }}

How to update checkbox field and option field in Laravel 5.2?

I can update text field of a form but i can not update checkbox field,option field and file field in Laravel 5.2. I i going to update then i can see all text value is perfectly shown in update blade view but in option field, checkbox field i see it is default value its don't comes from database. Again if i update with another option then its not saving.What i have tried so far:
My Controller:
public function update(Request $request, $id=0)
{
//
$id = $request->input("id");
$product = Product::find($id);
$product->product_name = $request->input('product_name');
$product->product_storage = $request->input('product_storage');
$product->product_percentage = $request->input('product_percentage');
$product->can_draw = $request->input('can_draw');
$product->real_price = $request->input('real_price');
$product->product_image = $request->input('product_image');
$product->save();
$request->session()->flash('alert-info', 'Product Successfully Updated!');
return Redirect::to('admin/product/all');
}
My update.blade.php View:
<div class="form-group">
<label class="col-md-3 control-label">{{ trans('common.can_be_draw') }}</label>
<div class="col-md-9">
<div class="input-icon">
<i class="fa fa-money" aria-hidden="true"></i>
<select name="can_draw" class="form-control">
<option value="{{ $product->can_draw }}">{{ trans('common.open') }}open</option>
<option value="{{ $product->can_draw }}">{{ trans('common.close') }}close</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">{{ trans('common.real_prize') }}</label>
<div class="col-md-9">
<div class="input-icon">
<input type="radio" class="form-control" name="real_price" value="{{ $product->real_price }}"> {{ trans('common.yes') }}yes
<input type="radio" class="form-control" name="real_price" value="{{ $product->real_price }}" checked="checked"> {{ trans('common.no') }}no
</div>
</div>
</div>
<div class="form-group">
<label for="exampleInputFile" class="col-md-3 control-label">{{ trans('common.product_picture') }}</label>
<div class="col-md-9">
<input type="file" id="exampleInputFile" name="product_image" value="{{ $product->product_image }}">
<small>{{ trans('common.pic_summery') }}</small>
</div>
</div>
Just use this:
<input type="radio" {{$product->can_draw == 'Yes' ? 'checked' : ''}} class="form-control" name="real_price" value="Yes"> {{ trans('common.yes') }}yes
<input type="radio" {{$product->can_draw == 'Yes' ? '' : 'checked'}} class="form-control" name="real_price" value="No" > {{ trans('common.no') }}no
EDIT
I have edited the above*
You should change the value to Yes and No and use this to check if the value saved is Yes or No. It does not matter how much is real_price
your option field have same value ({{ $product->can_draw }})
<option value="value1"{{( $product->can_draw=='value1'?selected)}}>{{ trans('common.open') }}open</option>
<option value="value2 " {{( $product->can_draw=='value1'?selected)}}>{{ trans('common.close') }}close</option>
and for file field must use file not input:
$product->product_image = $request->file('product_image');
You just can save option value by requesting from select name.
In your example:
select name = "can_draw"
Just save value from request:
$product->can_draw = $request->can_draw;
For checkbox same:
$product->real_price = $request->real_price;
Not necessary indicate input() helper in request.
To retrieve all value for option and check box value, please use foreach methods:
#foreach($can_draws as $can_draw)
<option value="{{ $can_draw->id }}">{{ $can_draw->name}}</option>
#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