Can I Use 2 foreach from variable database in laravel? - php

I Want make foreach in behind loping
$aproval = Aproval_position::get();
$user = User::get();
return view('authentication.index_aproval', compact('user','aproval'));
this view blade
#foreach($aproval as $approv)
<label for="" class="col-lg-3">{{ $approv->name_matrix }}</label>
<select>
#foreach($user as $user)
<option value="">{{ $user->name }}</option>
#endforeach
</select>
#endforeach
but when I try to use foreach user inside looping aproval thats error,
I want use User foreach table 2x or more but can't,
can you give me recomended way to use that ?

Change this:
#foreach($user as $user)
<option value="">{{ $user->name }}</option>
#endforeach
To this:
#foreach($user as $item)
<option value="">{{ $item->name }}</option>
#endforeach
I hope that would solve your problem.

Related

how to run to foreach loops for single select tag laravel?

I want to show previously selected options as selected, I have two data sets.
blocks that are JSON encoded in a column with country codes.
countries list from the country table
I want to compare both and want to show countries selected that are present in users blocked colmn JSON encoded
$user = User::find(auth()->user()->id);
$blocks = json_decode($user->blocked);
#foreach( Countries::orderBy('country_name')->get() as $country )
<option value="{{$country->country_code}}" >{{ $country->country_name }}</option>
#endforeach
i tried following but it selects on 1 value either 1st or last
#foreach ($sundaysArray as $key => $value)
#foreach( Countries::orderBy('country_name')->get() as $country )
<option #if( $value == $country->country_code ) selected="selected" #endif value="{{$country->country_code}}" >{{ $country->country_name }}</option>
#endforeach
#endforeach
Thanks to #gert-b
$user = User::find(auth()->user()->id);
$sundaysArray = json_decode($user->blocked);
#foreach( Countries::orderBy('country_name')->get() as $country )
<option #if (in_array($country->country_code,$sundaysArray)) selected="selected" #endif value="{{$country->country_code}}" >{{ $country->country_name }}</option>
#endforeach
$selected = explode(",", $products->supplier_id);
<select name="supplier_id[]" multiple="multiple">
#foreach($suppliers as $supplier)
<option value="{{ $supplier->id }}" {{ (in_array($supplier->id, $selected)) ? 'selected' : '' }}>{{ $supplier->name}}</option>
#endforeach
</select>

laravel avoid multiple Select's Option in blade

simply in my controller i have this collections:
$category = Category::with('users')->find($id);
$users = User::with('roles')->get();
and in front end i try to check which users in $users is into $category->users, like with:
in Category model which that belong to User i have one or multiple stored users and i would like checked html option if each $category has user which that is into $user
<select class="form-control multiselect-filtering" multiple="multiple" name="users[]">
#foreach ($category->users as $guser)
#foreach ($users as $user)
<option value="{{$guser->id}}"
#if ($guser->id == $user->id)
selected="selected"
#else
''
#endif
>
{{$user->username}}
</option>
#endforeach
#endforeach
</select>
this code and compare works, but i have multiple <option> which they are maybe selected or not. how can i solve this issue?
You can loop over the collection which is in the $users variable, and use the contains method to check if the current user id in the loop is contained in the $category->users collection.
#foreach ($users as $user)
<option value="{{$user->id}}"
#if ( $category->users ->contains('id', $user->id))
selected="selected"
#endif
>
{{$user->username}}
</option>
#endforeach

Trying to get property 'service_type' of non-object in laravel it is not an array

I am getting error Trying to get property 'service_type' of non-object in laravel
controller
$services= Service::pluck('service_type', 'service_id');
return view('package', compact('services'));
View:
<select class="form-control" name="service_type" id="service_type" data-parsley-required="true">
#foreach ($services as $service )
<option value="{{ $service->service_id }}">{{ $service->service_type }}</option>
#endforeach
</select>
pluck is not working as expected. Tested with several process and it does not work. use select with get or simply get.
$services= Service::get(['service_type', 'service_id']);
$services= Service::select('service_type', 'service_id')->get();
//use anyone from the above
And in view:
#foreach ($services as $service )
<option value="{{ $service->service_id }}">{{ $service->service_type }}</option>
#endforeach
Your $services variable is not an object, hence you can access it's properties with object notation ->. To display them, alter your view to this:
<select class="form-control" name="service_type" id="service_type" data-parsley-required="true">
#foreach ($services as $service )
<option value="{{ $service['service_id'] }}">{{ $service['service_type'] }}</option>
#endforeach
</select>

How to call function for checking selected option in laravel blade.php file?

I create a fuction for checking selected option and I want to call it in the
for the selected part.
#foreach ($stocks as $stock)
<option value="{{ $stock->id }}" ..... > {{ $stock->name }}</option>
#endforeach
for the .... should be the place to call the function that I created but I don't know how to call it.
I tried :
#foreach ($stocks as $stock)
<option value="{{ $stock->id }}" {{ CheckCombo($stock->id, $product-
product_type }} > {{ $stock->name }}</option>
#endforeach
for the function CheckCombo, it returns 'selected' back.
When you loop the option values check it with the db value and if it gets a match echo seleceted
#foreach ($stocks as $stock)
<option value="{{ $stock->id }}" #if( ($stock->id) == ($product->
product_type)) selected #endif > {{ $stock->name }}</option>
#endforeach

Laravel 4 - Undefined variable in select list using Eloquent

I'm struggling to see why my select list is returning an error.
I have a nested foreach loop, the second loop which should display records by building_id. However it is this very id which it returns an undefined variable for ($building->id)
Is it the construction of the query which is at fault?
<select>
#foreach (Building::orderBy('title')->get() as $building)
<optgroup label="{{ $building->title }}"></optgroup>
#foreach (Floor::whereIn('id', function($query){
$query->select('floor_id')
->from('rooms')
->where('building_id', $building->id)->distinct();
})->get() as $floor)
<option value="{{ $floor->id }}">{{ $floor->description }}</option>
#endforeach
#endforeach
</select>
You are using a closure function which you have passed to Floor::whereIn
So this closure function can't recognize external variables like the variable / object ($building) that you wanted to use in it.
so you have to tell PHP to use this $bulding variable in this closure / anonymous function:
<select>
#foreach (Building::orderBy('title')->get() as $building)
<optgroup label="{{ $building->title }}"></optgroup>
#foreach (Floor::whereIn('id', function($query) use ($building){
$query->select('floor_id')
->from('rooms')
->where('building_id', $building->id)->distinct();
})->get() as $floor)
<option value="{{ $floor->id }}">{{ $floor->description }}</option>
#endforeach
#endforeach
</select>
So now the function can reconize this variable :)
Read more about it : http://php.net/manual/en/functions.anonymous.php
Try it like this
....
#foreach (Floor::whereIn('id', function($query) use ($building){
...

Categories