I have two DB tables as item and price.
In view file, foreach of items I have form element as
<input type="text" name="dc_{{$item->id}}" value="{{$price->dc_*}}">
I want * in value as dc_{{$item->id}} for an example like dc_4 or dc_7
I am unable to use {{ $price-> dc_{{$item->id}} }} for value as it is wrong syntax.
What's an alternative way to implement such logic?
You can do
<input type="text" name="dc_{{ $item->id }}" value="{{ $price->{ 'dc_' . $item->id } }}">
Related
i have a foreach loop whereby i want to concatenate the id of each loop to its id jquery.i have tried this but i get the the id for the first loop only.this is my code in the blade file.
#foreach ($rentalcategories as $category)
<div class="checkbox checkbox-success">
<input type="text" value="{{ $category->id }}" class="catid">
<input type="radio" name="rentalcategory" class="rentalcattitle" id="rentalcat{{ $category->id }}" value="{{ $category->id }}">
<label for="rentalcat" class="control-label"> {{ $category->rentalcat_title }}</label><span>({{ $category->rentalhses->count() }})</span>
</div>
#endforeach
this is my jquery code
var categoryid=$(".catid").val();
console.log(categoryid);
var hsecategory= $('#rentalcat' + categoryid).prop('checked') ? $('#rentalcat' + categoryid).value : ''
on the console logging the categoryid variable am only getting the first value only for the first loop but the other one it doesn't show.how can i get the id for each loop then concatenate to an id in each loop.
here in my code i want to get the value for each loop using the id instead of class.how can i achieve this
I think because the Id as incrementing within the loop you just need to find element by id that starts with rentalcat
so something like this
$('input[id^="rentalcat"]')
credit to : Find html element which id starts with
that should give all the instances so you will need to preform an each function to get out each instance value
$('input[id^="rentalcat"]').each(function( i ) {
console.log($(this).val())
});
you can then concatenate that values or do whatever you need with the id's in the each loop
I hope this helps
what I'm trying to do is a filter radio button. When a user apply the filter based on a specific year, it goes into the controller to filter data results based on that year and return back to the view with new values. However when the page refreshes, the value filtered by the year is reflected, just that the radio button is unchecked and the default All radio button is checked.
What I did was store the radio button's id in a Session at the controller based on value of which radio button has been clicked. If the id of the radio button matches the id stored in the session, it should checked that radio button.
Index.blade.php
<label>
<input type="radio" name="year" id="tab" value="_all" class="flat-red filter" checked="checked"> All
</label>
#foreach ($years as $year)
<label>
<input type="radio" name="year" id="id{{$year->year}}" value="{{ $year->year }}" class="flat-red filter" checked = {{ Session::get('radio') == "id{{$year->year}}" ? 'checked' : 'false' }}> {{$year->year}}
</label>
#endforeach
<button type="submit" class="btn btn-primary btn-sm pull-right">Apply Filter</button>
StatisticsController.php
session(['radio' => 'id'.request('year')]);
return view('statistics.index', $counts, compact('years'));
Error that I am getting
syntax error, unexpected ')', expecting :: (T_PAAMAYIM_NEKUDOTAYIM)
Method(s) I have tried,
1. Using JS to see which radio button has been click and store it in the local session.
What am I doing wrong or missed out?
Everything inside of {{ }} is PHP. The {{ }} gets replaced by the Blade Compiler with something like this:
<?php echo e(...); ?>
So basically just use regular PHP syntax between {{ }}:
{{ Session::get('radio') == "id{$year->year}" ? 'checked' : 'false' }}
Having {{ }} inside of {{ }} is going to break the compiling of that echo statement.
A solution could be to read that session variable in your controller and inject it into your template.
Don't put more logic into the template, that's not what a template engine is made for - better decouple such logic. This helps you to keep your code maintainable. A blade template does not need to know anything about a session
I have many <input> like this:
<input type="number" id="{!! $item->id !!}" name="function_count[]" value="{{ $item->value }}"
In Controller, I can get an array of each input value, with:
$inputs = $request->input('function_count');
But also I need to get the id="{!! $item->id !!}" in the same array, because I need the data related (id and value) in the Controller. How could I do that?
You cannot pass two values via one input field. However, to achieve what you want, you can have different approaches.
Recommended Approach:
I am assuming, your function_count[] hasMany id. Therefore, you can create associative array as your input name like this:
<input type="number" name="function_count[{!! $item->id !!}]" value="" />
Later you can,
$inputs = $request->input('function_count');
foreach($inputs as $id=>$value){
// $id being the content of $item->id
// $value being the content of value=""
}
Approach #2:
If your id="{!! $item->id !!}" has client-side importance and you want to keep the attribute, you can also combine the data right before the form is submitted like this:
$("form").submit( function () {
$(this).find("input[type=number]").each(function(){
$(this).val($(this).attr("id") + ":" + $(this).val());
});
return;
});
Later you can,
$inputs = $request->input('function_count');
foreach($inputs as $input){
$contents = explode(":",$input);
// $contents[0] being the content of $item->id
// $contents[1] being the content of value=""
}
I am trying to update some data in my database from a form with this foreach loop:
$players_ids = $_POST['player_ids'];
foreach($players_ids as $player_id) {
foreach($_POST['goals'] as $goals) {
var_dump($goals);
$updateGoals = \DB::table('stats')->where('id', $player_id)->update(['goals' => $goals]);
}
foreach($_POST['assists'] as $assists) {
var_dump($assists);
$updateAssists = \DB::table('stats')->where('id', $player_id)->update(['assists' => $assists]);
}
foreach($_POST['tackles'] as $tackles) {
var_dump($tackles);
$updateTackles = \DB::table('stats')->where('id', $player_id)->update(['tackles' => $tackles]);
}
}
Here is the form:
#foreach($players as $player)
<tr>
<input type="hidden" name="player_ids[]" value="{{ $player->id }}">
<input type="hidden" name="game_id" value="{{ $game->id }}">
<td>{{ $player->fn }} {{ $player->ln }}</td>
<td><input type="number" value="0" name="goals[]" placeholder="goals"></td>
<td><input type="number" value="0" name="assists[]" placeholder="assists"></td>
<td><input type="number" value="0" name="tackles[]" placeholder="tackles"></td>
</tr>
#endforeach
When I try to submit this data, instead of passing through the individual stats of each player to the database, it only passes through the data of the last player in the foreach loop to every player. So if the final player has 3 goals, everyone will get 3 goals in the database. This is obviously not how I would like this to function.
I know soon after this I will run into an issue with updating the data in my database, because I'm not looking to override what is already in the database, but instead add the value passed through to the value already in the database, so if anyone has any knowledge on that as well would be greatly appreciated.
Your form has multiple copies of elements with the same name. Due to this, it only uses the last element with that name.
To utilize an array of values, you will need to use [] like you did for player_ids.
The reason this is happening is you are updating each player with the very last value set for goals, assists, and tackles. (Actually you are updating each player with every set for goals, assists and tackles, resulting in many queries.) You might need to change up your inputs so you know exactly what to update each player with.
This will also result in less looping and fewer queries.
#foreach($players as $player)
<tr>
<input type="hidden" name="player_ids[]" value="{{ $player->id }}">
<input type="hidden" name="game_id" value="{{ $game->id }}">
<td>{{ $player->fn }} {{ $player->ln }}</td>
<td><input type="number" value="0" name="goals[{{ $player->id }}]" placeholder="goals"></td>
<td><input type="number" value="0" name="assists[{{ $player->id }}]" placeholder="assists"></td>
<td><input type="number" value="0" name="tackles[{{ $player->id }}]" placeholder="tackles"></td>
</tr>
#endforeach
$players_ids = $_POST['player_ids'];
foreach ($players_ids as $player_id) {
$updates = \DB::table('stats')->where('id', $player_id)->update([
'goals' => $_POST['goals'][$player_id],
'assists' => $_POST['assists'][$player_id],
'tackles' => $_POST['tackles'][$player_id]
]);
}
This is untested, but if you want it to be additive in your database and not overwrite past values, you might be able to do something like this...
$players_ids = $_POST['player_ids'];
foreach($players_ids as $player_id) {
$updates = \DB::table('stats')->where('id', $player_id)->update([
'goals' => $_POST['goals'][$player_id] . '+' . DB::raw('goals'),
'assists' => $_POST['assists'][$player_id] . '+' . DB::raw('assists'),
'tackles' => $_POST['tackles'][$player_id] . '+' . DB::raw('tackles')
]);
}
Generally a better route would be to not make them additive though, and then you'd just use sum() to figure out how many goals, tackles, and assists each player has. Then you'd be able to do additional filtering on that like goals by date, goals by game, etc...
I am using select2 for selection of users, I am populating select2 according to its value in database, I am doing it like this :
<input class="form-control" data-tag-select="director" remote-url="{{ route("query::applies")}}" name="agency_director" type="hidden" value="{{ $team->name }}">
In the value simply i am adding the variable {{ $team->name }} which have the value but nothing comes up.
What am i missing ?