Multiselect Box with Laravel and values from database - php

I am having difficulty with multi-select boxes with Laravel, particularly, reading values from the database and displaying these values so they can be edited in my edit view.
In my controller, in the store function, I am using the implode function to save positive integers into a database field.
Here is the implode function
$prodmulti = implode(',', $prodmulti);
The database stores these values in a cell such as
prodmulti
1,2,3
This works fine and I get the cell, populated with values fine.
The problem is when I wish to edit these values.
If I have nothing saved in this cell, then as expected, I have nothing in the multi-select box which is good.
If I have 1 number in the database cell, then again, it is fone, I can see the corresponding item in the multi-select box.
If I have 2 or more items in the database cell, then the multi-select box shows as blank in the edit view. This is where I am having my problem.
The view blade for the edit page with the multi-select box looks like this:
<div class="col-lg-8 col-xl-5">
<div class="form-group">
<select class="js-select2 form-control" name="prodmulti[]" style="width: 100%;" multiple>
<option></option>
#foreach($productcategories as $productcategory)
<option value="{{ $productcategory->id }}" {{ $user->prodmulti == $productcategory->id ? "selected" : ""}}>
{{ $productcategory->name }}
</option>
#endforeach
</select>
</div>
</div>
And the show function in the controller looks like this where I use the explode to extract the values as an array as such.
$prodmulti = explode(',', $prodmulti);
return view(
'admin.users.edit',
compact([
'user',
'roles',
'countries',
'states',
'productcategories',
'prodmulti'
])
);
If it helps, here is my create a view where I again use one of these multi-select boxes to add values
<div class="col-lg-8 col-xl-5">
<div class="form-group">
<select class="js-select2 form-control" id="prodmulti" name="prodmulti[]" style="width: 100%;" data-placeholder="Choose many.." multiple>
<option></option>
#foreach($productcategories as $productcategory)
<option value="{{ $productcategory->id }}">
{{ $productcategory->name }}
</option>
#endforeach
</select>
</div>
</div>
Finally, I am using select2 multi-select boxes, but I get the same result if I use just standard bootstrap multi-select boxes and have tried both, I just like the look of the select2 multi-select boxes.
Any help would be greatly appreciated,
Thanks,
Steve.

use in_array() to check
$prodmulti = explode(',', $prodmulti);
return view('admin.users.edit', compact(['user', 'roles', 'countries', 'states', 'productcategories', 'prodmulti']));
then in blade
<div class="col-lg-8 col-xl-5">
<div class="form-group">
<select class="js-select2 form-control" id="prodmulti" name="prodmulti[]" style="width: 100%;" data-placeholder="Choose many.." multiple>
<option></option>
#foreach($productcategories as $productcategory)
<option value="{{ $productcategory->id }}"
{{ in_array($productcategory->id,$prodmulti) ? "selected" : "" }}>
{{ $productcategory->name }}
</option>
#endforeach
</select>
</div>
</div>

Related

How to get data from db to layout in Laravel 8.*

So I know how to retrieve data to views(using classes in controller)
but how can I return some data to layout which doesnt(or rather i dont know where they are) have its classes where i could get code to do so.
here is my form in which i would like to put cities name.
I always do it by giving to my frontend controller data from db using
$events = DB::select('select * from events');
return view('frontend/index', ['event' => $events]);
Edit
How to pass $city->id from loop to action?
My app.blade form:
<form method="POST" action="{!! route('cityView', ['id' => $city->id]) !!}" class="form-inline">
<div class="form-group">
<select name="city_id" class="form-control">
<option>xd</option>
#foreach(\App\Models\City::all() as $city)
<option value="{{ $city->id }}">{{ $city->name }}</option>
#endforeach
</select>
</div>
<button type="submit" class="btn btn-info">Search</button></a>
</form>
But how can I do it for a layout?
Would making a layout controller help with it? Or maybe make another class inside existing controller and return view of layout view?
Second solution would be adding this form to every single view since there will be only 3 of them but i prefer solution with app.blade
You can call data directly from layout something like this:
<form method="POST" action="#" class="form-inline">
<div class="form-group">
<select name="room_size" class="form-control">
<option></option>
#foreach(\App\Models\City::all() as $city)
<option value="{{ $city->id }}">{{ $city->name }}</option>
#endforeach
</select>
</div>
<button type="submit" class="btn btn-info">Search</button>
</form>

How to return value from different cell of table column by foreign key id in Laravel 8.*

I made view that adds edits data, to database, its table got foreign keys. So for request I need its id. Can i somehow display name of it while under will be ids?
Im sorry if title is missleading, i dont know how to ask about this properly
In my controller im using both tables for foreign and master keys
Im doing it using this: (i dont know how its called)
Form code
<div class="form-group col-md-4">
<label for="inputState">City</label>
<select id="inputState" name="city_id" class="form-control">
<option selected >{{ $data->city_id }}</option>
#foreach($city as $city)
<option>{{ $city->id }}</option>
#endforeach
</select>
</div>
I think this is you're looking for. On edit, you pass the $data and $city properties to blade and on loop you can make selected the option in select element if the condition is checked
<div class="form-group col-md-4">
<label for="inputState">City</label>
<select id="inputState" name="city_id" class="form-control">
<option value="">Select</option>
#foreach($city as $city)
<option value="{{ $city->id }}" #if($city->id == $data->city_id) selected #endif>{{ $city->name }}</option>
#endforeach
</select>
</div>

Show selected values and also other values in multi-select list

I'm trying to display selected values in multi-select when I try to edit the page
I want to show the list of all data but data should no-repeat show selected and then all other data
I have two arrays in view one is selected values and the other arrays have all values so how can I apply a condition in foreach loop that not repeat the values
My laravel blade code is:
#if( Auth::user()->role == "admin" || Auth::user()->role == "managers" )
<div class="form-group">
<label for="email">Välj hanterare<span class="required">*</span></label>
<select class="selectpicker form-control" name="handler_id[]" multiple data-live-search="true"
id="sel1">
#foreach($selected_handlers_names as $item)
<option value="{{$item->id}}" selected="selected">{{$item->name}}</option>
#endforeach
#foreach($handlers as $items)
<option value="{{$items->id}}">{{$items->name}}</option>
#endforeach
</select>
#if ($errors->has('handler'))
<span class="text-danger">{{ $errors->first('handler') }}</span>
#endif
</div>
#endif

Laravel Dropdown Form Get Form Database

I am using laravel 5.4 . I am trying to get data form Department table and data come also the problem there more dropdown menu come also please tell how can i solve this problem. Thanks advance.[
<div class="form-group">
{{Form::label('department','Department')}}
#foreach($department as $value)
{{Form::select('department',[$value->name], ' ', array('placeholder' => 'Select Department','class'=>'form-control'))}}
#endforeach
</div>
It's not entirely clear what you're after. Your current code seems to output several select boxes, one for each department and my best guess is that you want to output only one select box, with each department as an option in it. You can achieve that by doing something like this (You may have to adapt this code slightly to suit your needs):
The manual approach
<div class="form-group">
<label for="department">Department</label>
<select id="department" name="department" class="form-control">
<option value="">Select Department</option>
#foreach ($departments as $department)
<option value="{{ $department->id }}">{{ $department->name }}</option>
#endforeach
</select>
</div>
Using Laravel Collective's Form Builder
<div class="form-group">
{!! Form::label('department', 'Department') !!}
{!! Form::select('department', ['' => 'Select Department'] + $departments->pluck'name', 'id')) !!}
</div>
Try to do this way :-
<div class="form-group">
{{Form::label('department','Department')}}
<select class="form-control" name="department">
#foreach($department as $value)
<option value="$value">$value</option>
#endforeach
</select>
</div>

Access old("value") in form edit

I'm currently working on an edit page. My edit perfectly shows the old (product name) input value, like so:
<div class="form-group">
<label for="product-input">#lang('product.name')</label>
<input id="product-input" type="text" name="name" class="form-control" value="{{ isset($product) ? $product->name : '' }}">
</div>
My question is, how can I do this with a select? How can I get the old selected option value when editing?
My code:
<div class="form-group">
<label>#lang('product.group')</label>
<select name="product_group_id" id="product_group_id" class="form-control" ng-model="productGroup" ng-change="changedType(val)">
<option style="display: none" value="">#lang('product.choose_group')</option>
#foreach ($product_groups as $product_group)
<option value="{{$product_group->id}}">{{$product_group->name}}</option>
#endforeach
</select>
</div>
Solution:
Added ng-selected to the option element, like so:
#foreach ($product_groups as $product_group)
<option value="{{$product_group->id}}" ng-selected="{{ isset($product->product_group_id) ? $product->product_group_id == $product_group->id : ''}}">{{$product_group->name}}</option>
#endforeach
You need to check if the old value equals option value and then use selected attribute:
#foreach ($product_groups as $product_group)
<option #if(old('product_group_id') == $product_group->id) selected #endif value="{{$product_group->id}}">{{$product_group->name}}</option>
#endforeach
Even better considering the first time with default value:
<option
value="{{$product_group->id}}"
{{ old('product_group_id', $product->id) != $product_group->id?: 'selected' }}>
{{$product_group->name}}
</option>

Categories