I have this code:
$arrayWithSelectedValues = [[user_id] => 3,[language_id] => 2], [user_id] => 3,[language_id] => 12]]
And I have checkbox :
#foreach($languages as $language)
<fieldset>
<input type="checkbox"
class="icheckbox_square-red"
id="input-15" name="languages[]"
value="{{ $language->id }}">
<label for="input-15">{{ $language->name }}</label>
</fieldset>
#endforeach
How can I mark checkboxes as marked - those that have language_id from the array = $ language-> id?
Use the php function in_array() to check if the $language_id exists in the array of $languages
#foreach($languages as $language)
<fieldset>
<input type="checkbox"
class="icheckbox_square-red"
id="input-15" name="languages[]"
value="{{ $language->id }}"
#if(in_array($language_id, $languages))
checked
#endif
>
<label for="input-15">{{ $language->name }}</label>
</fieldset>
#endforeach
If $language->id value you are looking for, and you create an array of checked language ids from $arrayWithSelectedValues you can do:
<input
type="checkbox"
#if(in_array($language->id, $languages))
checked
#endif
>
You can collected the language IDs like so:
$languages = [];
foreach ($arrayWithSelectedValues as $selectedValues) {
$languages[] = $selectedValues['language_id'];
}
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 added a drop down list in the user registration form in Laravel. Now I want to make a form to edit it but I'm stuck.
What I did is I made a drop down list which a new user can register where he/she lives. I used a list of cities(pref.php) in the configuration folder to make this drop down list. I want the user to be able to see the cities that they have registered default when opening the edit form and then change it or leave it alone but I couldn't figure out how to do this.
Here are my codes.
editprofile.php
<form action="/profile" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
{{ method_field('patch') }}
<p>
<label><b>name</b><br>
<input type="text" name="name" value='{{ $user->name }}'><br>
</label>
</p>
<p>
<label><b>location</b><br>
<select type="text" name="location" value="{{ $user->prefName }}">
#foreach(config('pref') as $key => $score)
<option value="{{ $key }}">{{ $score }}</option>
#endforeach
</select>
</label>
</p>
<p>
<label><b>mail adress</b><br>
<input type="text" name="email" value='{{ $user->email }}'><br>
</label>
</p>
<p>
<label><b>profile image</b><br>
<input type="file" name="profile_image"><br>
</label>
</p>
<input type="hidden" name="id" value="{{ $user->id }}">
<input type='submit' value='edit' class="btn btn-primary mt-3">
</form>
ProfileController.php
public function edit()
{
$user = Auth::user();
return view('profile.editprofile', ['user' => $user]);
}
public function update(ProfileRequest $request, User $user)
{
$user = User::find($request->id);
$user->name = $request->name;
$user->email = $request->email;
$user->location = $request->location;
if ($request->hasFile('profile_image')) {
Storage::delete('public/profile_image/' . $user->profile_image);
$path = $request->file('profile_image')->store('public/profile_image');
$user->profile_image = basename($path);
}
$user->update();
return redirect('/profile');
}
pref.php
<?php
return array(
'0' => 'not selected',
'1' => 'New York',
'2' => 'Los Angeles',
'3' => 'Dallas'
);
I thought this page had a similar problem but it was a little different.
Any help would be appreciated as I have tried multiple methods with no success.
Thank you in advance.
to make an option selected you have to put selected property in that option. putting value on select won't work. you can do it like
<select type="text" name="location">
#foreach(config('pref') as $key => $score)
<option value="{{ $key }}" {{ $user->location == $key ? 'selected' : '' }}>{{ $score }}</option>
#endforeach
</select>
In your edit form you had a particular user information by it ID in one array after that you get the user into a edit page right, using that array you can check with option box like below example
<select type="text" name="location" value="{{ $user->prefName }}">
#foreach(config('pref') as $key => $score)
<option <?php echo($get_array_by_user_id->table's_exist_location_value == $key) ? 'selected' : ''; ?> value="{{ $key }}">{{ $score }}</option>
#endforeach
</select>
I think it is working exactly to you : - )
Thank You,
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
I have a students_record table and i am saving my data in json format there
$students_data = Students::findOrFail($id);
$attributes = DB::table('studentsdata')->where('id', $student_data->id)->select('attributes')->first();
$result = json_decode($student_data->meta_attributes);
echo "<pre>";
print_r($result);
die;
I am getting result as
stdClass Object
(
[name] => test
[status] => Suscess
[country] => Array
(
[0] => Pakistan
[1] => USA
)
[days_time] => months
)
I passed result to view and i an getting first name simply as
<label> Name</label>
<div class="col-md-8">
<div class="input-icon right">
<input type="text" name="name" value="{{isset($result->name) ? $result->name : '' }}" />
</div>
</div>
Its giving my my name in that field fine now
i want to get countries array also in my country field All countries in that array please Help how can i get it here which i have in $result as i show input above against my query
<label> Country </label>
<div class="col-md-8">
<div class="input-icon right">
<select id="country" multiple name="country[]" onchange="
getValue('opens-clicks-region', 'student_tracking', 'region', 'country', this.value, this.id)" disabled="disabled">
<option value=""></option>
</select>
</div>
</div>
You can use a foreach loop to generate <option> tags, if you are using laravel blade its much easier
#foreach ($result->country as $country)
<option value="{{ $country }}"> {{ $country }} </option>
#endforeach
take a look at the blade documentation here : https://laravel.com/docs/5.3/blade#loops
Try some thing like this
<select id="country" multiple name="country[]" onchange="getValue('opens-clicks-region', 'student_tracking', 'region', 'country', this.value, this.id)" disabled="disabled">
#if(!empty($result->country))
#foreach($result->country as $key => $country)
<option value="{{ $key }}">{{ $country }}</option>
#endforeach
#else
<!-- Default case if there is no country -->
<option value="default_key">Default value</option>
#endif
</select>
FYI: Laravel blade has a check for isset input field
You can replace
<input type="text" name="name" value="{{isset($result->name) ? $result->name : '' }}" />
with
<input type="text" name="name" value="{{ $result->name or '' }}" />
Hope it helps!
I'm learning Laravel 5.2 for first time, and I need to repopulate a form when the validation fails.
I was able to do so with single inputs as 'name' or 'description', but I couln't find out how to do it with the 'category' which is a multiple select.
View:
<form action="{{ url('addproduct') }}" method="POST">
{!! csrf_field() !!}
<div class="form-group">
<label for="#productName">Name:</label>
<input id="productName" class="form-control" name="name" type="text" placeholder="Product name" value="{{ old('name') }}">
</div>
<div class="form-group">
<label for="#productDescription">Description:</label>
<textarea id="productDescription" class="form-control" name="description" rows="3" placeholder="Product description" value="{{ old('description') }}"></textarea>
</div>
#if ($categories)
<div class="form-group">
<label for="#productCategory">Category:</label>
<select id="productCategory" class="form-control" name="category[]" multiple>
#foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>
#endif
<div class="form-group">
<label for="#productQuantity">Quantity:</label>
<input id="productQuantity" class="form-control" name="quantity" type="number" min="0" value="0">
</div>
<button class="form-control btn btn-success" type="submit">Add Product</button>
</form>
Controller:
public function add(Request $request) {
$data = array();
$data['msgAdded'] = false;
$data['categories'] = Category::select('name', 'id')->get();
if ($request->isMethod('post')) {
$this->validate($request, [
'name' => 'required|max:50',
'description' => 'required|max:200',
'category' => 'required',
'quantity' => 'required|min:0',
]);
$product = new Product;
$product->name = $request->name;
$product->description = $request->description;
$product->quantity = $request->quantity;
$product->save();
foreach ($request->category as $cat) {
$category = Category::find($cat);
$category->products()->attach($product->id);
}
$data['msgAdded'] = true;
}
$data['view'] = 'addproduct';
return view('products/add', $data);
}
I would like to know if it can be achieve with blade, or the best way to achieve that.
Thank you!
Option 1
You would have to write the select part of your form like this:
<div class="form-group">
<label for="#productCategory">Category:</label>
<select id="productCategory" class="form-control" name="category[]" multiple>
#foreach ($categories as $category)
<option value="{{ $category->id }}"{{ !empty(old('category')) && in_array($category->id, old('category')) ? ' selected="selected"' : '' }}>{{ $category->name }}</option>
#endforeach
</select>
</div>
Option 2
And arguably the more elegant solution is to install the html package from the laravel collective
composer require laravelcollective/html
follow the installation instructions on the laravel collective Forms & HTML documentation.
then just enter this where your select would be:
{!! Form::select('category[]', $categories, old('category'),['class' => 'form-control', 'multiple' => 'multiple'] ) !!}