Laravel 4 - Undefined variable in select list using Eloquent - php

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){
...

Related

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>

Can I Use 2 foreach from variable database in laravel?

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.

foreach in foreach laravel

I pass two array from controller to view and then i want option to select when the value is the same. But i want to show all data from $arr_1 in select. and my result i get duplicate data in my select.
$arr_1=["1","2","3","4"];
$arr_2=["1","2","4"];
#foreach($arr_1 as $val)
#foreach($arr_2 as $value)
#if($val==$value)
<option selected>{{$val}}</option>
#else
<option>{{$val}}</option>
#endif
#endforeach
#endforeach
Any solution for these?
It can be done without two foreach
$arr_1=["1","2","3","4"];
$arr_2=["1","2","4"];
<select multiple>
#foreach($arr_1 as $val)
#if(in_array($val,$arr_2))
<option val="{{$val}}" selected>{{$val}}</option>
#else
<option val="{{$val}}" >{{$val}}</option>
#endif
#endforeach
</select>
Demo

Getting data out of array separately in laravel

I'm using shipping system where will calculate shipping price.
this is what i get in my blade currently:
As you see I get id of state and then name of state.
here is my function:
public function index() {
$data = RajaOngkir::Provinsi()->all();
return view('welcome', compact('data'));
}
and this is my blade code:
#foreach($data as $sss)
#foreach($sss as $numbers)
{{ $numbers }} <br>
#endforeach
#endforeach
here is {{$data}} dump info:
what I want is to getting id and name separately so I can use it
in input.
The foreach will loop through anyhow and pull the relevant info. You just then show it within your blade like so:
#foreach ($data as $info)
ID: {{ $info->province_id }} <br />
Name: {{ $info->province }}
#endforeach
Updated:
From my re read you want it as a form drop down select so you can calculate shipping fee's therefore you'd simply do:
<select name="province" id="">
<option value="">Select</option> -> This will be the default select option
#foreach ($data as $info)
<option value="{{ $info->province_id }}">{{ $info->province }}</option>
#endforeach
</select>

Categories