I try looping variable with foreach in laravel but I get error
syntax error, unexpected ')'
Here my controller
public function getAdd()
{
$tahun_awal_bgt = 1990;
$tahun_skr_bgt = date('Y');
return view('laporan.posisikeuangan_laporan')->with([
'tahun_awal_bgt' => $tahun_awal_bgt,
'tahun_skr_bgt' => $tahun_skr_bgt]);
}
This my views
<div class="col-sm-2">
<select class="form-control">
<option>-- Semua --</option>
#foreach($tahun_awal_bgt <= $tahun_skr_bgt)
<option value="{{ $tahun_skr_bgt }}">{{ $tahun_awal_bgt }}</option>
{{ $tahun_awal_bgt++ }}
#endforeach
</select>
</div>
I still dont understand where I'am doing wrong, how do I fix this ?
Change this line in thr blade
#foreach($tahun_awal_bgt <= $tahun_skr_bgt)
To
#foreach($tahun_awal_bgt as $tahun_skr_bgt)
You should use for loop in your case:
#for($tahun_awal_bgt = $tahun_awal_bgt; $tahun_awal_bgt <= $tahun_skr_bgt; $tahun_awal_bgt++)
<option value="{{ $tahun_skr_bgt }}">{{ $tahun_awal_bgt }}</option>
<!-- No need for $tahun_awal_bgt++ here, i added in for loop -->
#endfor
Please note that i used #for and #endfor instead of #foreach and #endforeach, further there was no need to do {{ $tahun_awal_bgt++ }} in loop block is I already added it in #for loop declaration.
You should also learn how to use for and foreach loop in php
https://www.w3schools.com/php/php_looping_for.asp
Based on your confirmation at the comment before, you can follow this code to fix your problem.
#for($tahun_skr = $tahun_skr_bgt; $tahun_skr >= $tahun_awal_bgt; $tahun_skr--)
<option value="{{ $tahun_skr }}">{{ $tahun_skr }}</option>
#endfor
Related
I found a bug when i try to displayed data on select2 tag. I got duplicate data and I want to know how to fixed it. Im using laravel 7 and here's my code
This is my controller code:
public function editLocation(Request $request,$id){
$mtFasilitas = DB::table('MT_Facility')->select('id','name')
->get();
$trFasilitas = DB::table('TR_Fasilitas')->select('idMtFasilitas')
->where('idDetailLokasi',$id)
->get();
return view('layout.back.content_kos.edit_kos',['mtFas' => $mtFasilitas, 'trFas' => $trFasilitas]);
}
This is my blade template :
<select class="js-example-basic-multiple form-control mb-4" name="fasilitas[]" multiple="multiple">
#foreach ($mtFas as $key => $data)
#foreach ($trFas as $key2 => $data2)
<option value="{{$data->id}}"{{$data2->idMtFasilitas == $data->id ? 'selected="selected"' : ''}}> {{ $data->name}}</option>
#endforeach
#endforeach
</select>
And this is the result :
enter image description here
Anyone can help me ?
Thank you
$trFasilitas = DB::table('TR_Fasilitas')->select('idMtFasilitas')
->where('idDetailLokasi',$id)
->get()
->pluck('idMtFasilitas')
->all();
<select class="js-example-basic-multiple" name="fasilitas[]" multiple="multiple">
#foreach ($mtFas as $data)
<option value="{{ $data->id }}" {{in_array($data->id, $trFas) ? 'selected="selected"' : '' : ''}}>
{{ $data->name }}</option>
#endforeach
</select>
I think this should do.
This is the result of your updated answer sir #Andy Song
As per your data
$mtFasilitas = [{"id":4,"name":"a"},{"id":5,"name":"b"},{"id":6,"name":"c"}]
$trFasilitas = [{"idMtFasilitas":4},{"idMtFasilitas":5}]
Your inner loop running twice for each single value of outer loop, thats why data printed twice.
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>
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
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.
I have multiple select box with same name inside loop in a form. I have added laravel validation to check select box selected or not. But validation error message is showing for all the select boxes. Please check image attached. Any help would be appreciated.
cart.blade.php
#forelse($carts as $key => $cart)
<div class="col-sm-4 col-sm-4-booking1 form-group {{ $errors->has('guest.*.sleeps') ? ' has-error' : '' }}">
<label>Sleep(s)</label>
<select class="form-control form-control-booking1 jsBookCalSleep" name="guest[{{ $cart->_id }}][sleeps]">
<option value="">Choose Sleep(s)</option>
#for($i = 1; $i <= 30; $i++)
<option value="{{ $i }}" #if($i == $cart->sleeps) selected #endif>{{ $i }}</option>
#endfor
</select>
#if ($errors->has('guest.*.sleeps'))
<span class="help-block"><strong>{{ $errors->first('guest.*.sleeps') }}</strong></span>
#endif
</div>
#empty
<p>No bookings in your cart</p>
#endforelse
CartController.php
public function store(CartRequest $request)
{
dd($request->all());
}
CartRequest.php
public function rules()
{
return [
'guest.*.sleeps' => 'required|not_in:0'
];
}
Try these
#php
$inputName='guest.'.$cart->_id.'.sleeps';
#endphp
#if ($errors->has($inputName))
<span class="help-block"><strong>{{ $errors->first($inputName) }}</strong></span>
#endif
I didn't tested it,
If not working let me know
Solution: We can solve this issue by using two function. a.collect and b. contains. See the example below.
<select class="form-control m-bootstrap-select m_selectpicker"
name="generic_ids[]" data-live-search="true" multiple>
<option value="">Select Generic</option>
#foreach($generics as $generic)
<option value="{{$generic->id}}"
#if(collect(app()->request->generic_ids)->contains($generic->id))
selected
#endif
>{{$generic->title}}</option>
#endforeach
</select>
Code
Output