Unique/distinct in laravel foreach - php

In laravel, I have a dropdown select box with some values, but my foreach loop around the box is showing every instance of $psku->frame_desc, as it should. However, I want it to only show distinct values.
Here's the code:
<select style="margin-top:10px; max-width:200px;" >
<option value="" selected data-default>Sort by type:
</option>
#foreach ($orderFormData->pgroups as $pgroup)
#foreach ($pgroup->pskus as $psku)
<option value="{{ $psku->frame_desc }}">{{ $psku->frame_desc }}</option>
#endforeach
#endforeach
What's the best way to declare distinct or unique values within a foreach like this in laravel?

Assuming these are collections, you can do:
#foreach ($orderFormData->pgroups as $pgroup)
#foreach ($pgroup->pskus->unique('frame_desc') as $psku)
<option value="{{ $psku->frame_desc }}">{{ $psku->frame_desc }}</option>
#endforeach
#endforeach
Documentation
If they are not collections, you can make them collections:
#foreach ($orderFormData->pgroups as $pgroup)
#foreach (collect($pgroup->pskus)->unique('frame_desc') as $psku)
<option value="{{ $psku->frame_desc }}">{{ $psku->frame_desc }}</option>
#endforeach
#endforeach

Related

Cannot get old values of multi-select dropdown in laravel blade

How can I get selected old values of multi-select dropdown in the create form and the edit form?
<select name="team[]" id="team" class="selectpicker" multiple>
#foreach ($tdropdown as $tdrop =>$id)
<option value="{{$id}}">{{$tdrop}}</option>
#endforeach
</select>
I tried as follows in the create form:
<option value="{{ $id }}" {{ (collect(old( 'team' ))->contains($tdrop)) ? 'selected':'' }} {{ $tdrop,$employee->team ? 'selected' : ''}} > {{ $tdrop }}</option>
In the edit form:
<option value="{{ $id }}" {{ (collect(old( 'team' ))->contains($tdrop)) ? 'selected':'' }} {{ ($tdrop,$employee->team) ? 'selected' : ''}} > {{ $tdrop }}</option>
Please help me to get the multi-select old values in the edit form and create form. Thank you.

Laravel table in a form

I have this site with a form in it. Here I ask the user for their language skills.
First I built it like a list:
<div class="col-md-3">
<ul class="list-unstyled" id="request_profile_languages">
#isset($requestProfile)
#foreach($requestProfile->languages as $index=>$requestProfileLanguage)
<li>
<select {{ (FALSE == $canEdit) ? 'readonly':'' }} class="form-control form-select" id="{{ 'request_profile_language_'.$index }}" name="{{ 'request_profile_language_'.$index }}" placeholder="Language">
#foreach($languages as $r=>$language)
<option {{ ($language->name == $requestProfileLanguage->language->name) ? 'selected':'' }} value="{{ $language->value }}" >{{ $language->name }}</option>
#endforeach
</select>
</li>
#endforeach
#else
<li>
<select {{ (FALSE == $canEdit) ? 'readonly':'' }} class="form-control form-select" id="request_profile_language_0" name="request_profile_language_0" placeholder="Language">
#foreach($languages as $r=>$language)
<option {{ ($language->name == 'English') ? 'selected':'' }} value="{{ $language->value }}" >{{ $language->name }}</option>
#endforeach
</select>
</li>
#endisset
</ul>
</div>
There are more divs with language level and language remove.
The problem is on site, when I minimize the browser, it looks like this:
Language List
I want a table replacing this list. What am I supposed to do?
Well, you have your list in a .col-md-3 div. If you change it to a .col-xs-something and put the x's in a .column-xs beside it, then they should still match up. I'm assuming that's how it is on larger devices since you didn't post the html for the x's?

Editing drop-down doesn't fetch value

I have created a drop-down for categories and subcategory. It works fine when i submit the form, but when I edit the form, category field does not come with refilled data from the database, category drop-down come like it show in create form.
here is my edit:
<div class="form-group">
{!! Form::label('category','Category:') !!}
<select name="category" id="category" class="form-control input-sm">
#foreach($s as $k)
<option value="{{ $k['id'] }}">{{ $k['name'] }}</option>
#endforeach
</select>
</div>
<div class="form-group">
{!! Form::label('subcategory','Subcategory:') !!}
<select name="subcategory" id="subcategory" class="form-control input-sm">
<option value=""></option>
</select>
</div>
Controller:
public function edit($id)
{
// get the event
$event = Event::findOrFail($id);
$s = Category::all()->where('parent_id','=','0');
$r = Event::all();
$daysOfWeek = unserialize(Event::find($id)->days_of_week);
// show the edit form and pass the event
return view('event.edit',compact('event','s','r','daysOfWeek'));}
I haven't used relations for the dropdown, I have used jquery and ajax to select subcategory after I select category.
What can i do to get the value stored in database when I do edit form?
I got my answer thanks every one for help!
View:
<div class="form-group">
{!! Form::label('category','Category:') !!}
<select name="category" id="category" class="form-control input-sm">
#foreach($s as $k)
#if($k['id'] == $m)
<option value="{{ $k['id'] }}" selected="{{$m}}">{{ $k['name'] }}</option>
#else
<option value="{{ $k['id'] }}">{{ $k['name'] }}</option>
#endif
#endforeach
</select>
</div>
<div class="form-group">
{!! Form::label('subcategory','Subcategory:') !!}
<select name="subcategory" id="subcategory" class="form-control input-sm">
#foreach($subcat as $k)
#if($k['name'] == $subid)
<option value="{{ $k['id'] }}" selected="{{$subid}}">{{ $k['name'] }}</option>
#else
<option value="{{ $k['id'] }}">{{ $k['name'] }}</option>
#endif
#endforeach
<option value=""></option>
</select>
</div>
Controller:
public function edit($id)
{
$event = Event::findOrFail($id);
$s = Category::where('parent_id','=','0')->get();
$r = Event::all();
$m = Event::find($id)->category;
$subid = Event::find($id)->subcategory;
$subcat = Category::where('parent_id','=',$m)->get();
$daysOfWeek = unserialize(Event::find($id)->days_of_week);
return view('event.edit',compact('event','s','subcat','subid','r','daysOfWeek','m'));
}
There is a mistake in your Controller method edit()
Your code:
$s = Category::all()->where('parent_id','=','0'); // wrong
Isn't that supposed to be:
$s = Category::where('parent_id','=','0')->get(); // correct
You are already fetching the categories with all, and then passing the where condition, hence it is not showing you the results. where condition returns the instance of Illuminate\Database\Eloquent\Builder and not the desired results.
Update the wrong line with the correct one and the result should appear as you want.

Alternative to calling models in the views - Laravel

Trying to improve the code of my app and just migrated to L5. I used to call models in my views and I know this is not best practice - I should separate data calling and views completely.
However how do you deal with a situation like this:
<div class="field">
<label for="country">Country<sup>*</sup></label>
<select class="" id="country" name="country">
<option value="{{{ old('country') ? old('country') : '' }}}">{{{ old('country') ? App\Country::find(old('country'))->country_name : '' }}}</option>
#foreach ($countries as $studio_country)
<option value="{{ e($studio_country->id) }}">{{ e($studio_country->country_name) }}</option>
#endforeach
</select>
#if ($errors->has('country'))
<div class="alert alert-warning" role="alert">
{{ $errors->first('country') }}
</div>
#endif
<br>
</div>
Basically if there is an input and the input did not pass the validation the page refreshes with the old input and the error message. I need to extract the name of the country from the DB since I only have its ID.
You would want to do something like this:
<select id="country" name="country">
#foreach ($countries as $studio_country)
<option
value="{{ $studio_country->id }}"
{{ $studio_country->id === old('country') ? 'selected' : '' }}>
{{ $studio_country->country_name }}
</option>
#endforeach
</select>

Loop through two object in laravel

I have two arrays of objects and I want to check if ids of elements of both are the same then select option in select box otherwise just display it. I tried like this:
<select multiple class="form-control" name="category_id[]">
#foreach($mysliwski as $mysl)
#if(!$product->categories->isEmpty())
#foreach($product->categories as $cat)
#if($mysl->id == $cat->id)
<option selected value="{{$mysl->id}}">{{$mysl->name}}</option>
#else
<option value="{{$mysl->id}}">{{$mysl->name}}</option>
#endif
#endforeach
#else
<option value="{{$mysl->id}}">{{$mysl->name}}</option>
#endif
#endforeach
</select>
But it works only when one category is the same. When there are more my select option are duplicated.
Here are my two
arrays. Where I have bug?
You could achieve that like this
<select multiple class="form-control" name="category_id[]">
#foreach($mysliwski as $mysl)
#if(!$product->categories->isEmpty())
{{-- */$selected='';/* --}}
#foreach($product->categories as $cat)
#if($mysl->id == $cat->id)
{{-- */$selected='selected';/* --}}
#endif
#endforeach
<option {{ $selected }} value="{{$mysl->id}}">{{$mysl->name}}</option>
#else
<option value="{{$mysl->id}}">{{$mysl->name}}</option>
#endif
#endforeach
</select>
Note: {{-- */$selected='';/* --}} is a tricky way of declaring a variable in a blade template. See https://stackoverflow.com/a/17176876/170539

Categories