Loop through two object in laravel - php

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

Related

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>

Creating a drop down list in laravel to select a users role - laravel

I am creating a website using laravel and I have a form to create a new users and within it i need an option to display a way to select a users role, i would like to do this in the form of a drop down list but I'm having difficulty figuring it out.. so far I have this (see below my create.blade.php), but it is not displaying the data from the three roles i have in the table in the DB, (these are admin,instructor and student).
<div class="form-group">
<label for="role">Role</label>
<select name="roles[]" class="form-control">
#foreach($roles as $role)
<ul class="dropdown-menu" role="menu">
{{ $role }}
</ul>
#endforeach
</select>
</div>
Below is my form, I am new to laravel so just trying to learn to better myself, any help is much appreciated :)
If you're using native HTML select, you may use
<div class="form-group">
<label for="role">Role</label>
<select name="roles[]" class="form-control">
#foreach($roles as $role)
<option value="{{ $role->id }}"> {{ $role->nameOrWhatever }} </option>
#endforeach
</select>
</div>
In your UserController
/**
* Show the form for creating a new user
*
* #param \App\Role $model
* #return \Illuminate\View\View
*/
public function create(Role $model)
{
return view('users.create', ['roles' => $model->get(['id', 'name'])]);
}
then, in your create.blade.php
<select name="role_id" id="input-role" required>
<option value="">-</option>
#foreach ($roles as $role)
<option value="{{ $role->id }}" {{ $role->id == old('role_id') ? 'selected' : '' }}>{{ $role->name }}</option>
#endforeach
</select>
This will get you the desired

Unique/distinct in laravel foreach

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

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?

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>

Categories