I'm doing a filter-by specifications on an app, and was wondering how can I implement without reloading the whole page (which I'm trying to convert to AJAX rather than PHP/Laravel alone). However my filters are based off on foreach loops
#foreach($cores as $core)
<li style="list-style: none;">
<input type="checkbox" name="core[]" id="cores" class="form-check-control" value="{{ $core->value }}"> {{ $core->value }}
</li>
#endforeach
Am trying $('#cores').val() but it is not working. I'm thinking of call it by class name then looping it to check if it is checked but it may affect performance.
The for loop is creating multiple elements with the same id, so accessing it this way won't work. Remove the id element from the checkbox, and refer to this answer for getting the value of the checked boxes in JS
try to do it this way and u can take the values
#foreach($cores as $core)
<li style="list-style: none;">
<input type="checkbox" name="core[{{ $core->value }} || paste something unique for the checkbox]" id="cores" class="form-check-control" value="1"> {{ $core->value }}
</li>
#endforeach
after that u can foreach $core and take only values that are seted and filter by them
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 found a lot of similar questions but without a real solution for me.
I have a project on Laravel 5.4
Now, in my Controller I am preparing filters for my search.
So, Imagine that I want to filter my search by "location", and "about" fields of my Cv model.
So, I am finding distinct values of each one doing this
$arrOfTags = explode(',', $request['position']);
//separate basic search request(using some js modules that return a request with "," separator.
//Basically, find a Cv where location of "positions" is distinct
$filter0 = Cv::select('location')
->whereIn('position', $arrOfTags)
->groupBy('location')
->get();
$filter1 = Cv::select('about')
->whereIn('position', $arrOfTags)
->groupBy('about')
->get();
Now, I want to concatinate this 2 results in object named $filters
And then, in my Blade to use it like :
#foreach($filters as $filter)
<li> <input type="checkbox" name="location" value="{{$filter->location}}"/> <span> {{$filter->location}} </span> </li>
#endforeach
#foreach($filters as $filter)
<li> <input type="checkbox" name="location" value="{{$filter->about}}"/> <span> {{$filter->about}} </span> </li>
#endforeach
I tried array_merge of objects, creating a new empty object and set parameters of this objects and other stuff, but without success.
In this case you can use unique() collection method. Get the data:
$filters = Cv::select('location', 'about')
->whereIn('position', $arrOfTags)
->get();
Get unique values for each column and use results:
#foreach($filters->unique('location') as $filter)
<li> <input type="checkbox" name="location" value="{{ $filter->location }}"/> <span> {{$filter->location}} </span> </li>
#endforeach
#foreach($filters->unique('about') as $filter)
<li> <input type="checkbox" name="about" value="{{ $filter->about }}"/> <span> {{ $filter->about }} </span> </li>
#endforeach
Why are you just making it complected ?
$locationFilters= Cv::select('location')
->whereIn('position', $arrOfTags)
->groupBy('location')
->get();
$aboutFilters= Cv::select('about')
->whereIn('position', $arrOfTags)
->groupBy('about')
->get();
return view('view name',compact('locationFilters','aboutFilters'));
Then in your view:
#foreach($locationFilters as $filter)
<input value="{{$filter->location}}"/>
#endforeach
#foreach($aboutFilters as $filter)
<input value="{{$filter->about}}"/>
#endforeach
on the server side, you can do it as
$filters[] = $filter0->location;
$filters[] = $filter1->about;
on view, just print the value of the array instead of accessing the object.
You could simply use merge method to merge two collections. The merge method merges the given array or collection with the original collection. Try this-
$filters = $filter0->merge($filter1);
For further details you can see the official laravel documentation
Hi gladly I want to put the output of my rows from a table in on single text field. This is what I currently have in my view:
{{Form::label('tag', 'tags')}}
#foreach ($task->tagtask as $tt)
<input type="text" name="tag_name" class="form-control" value='{{ $tt->tag['tag_name'] }}'>
#endforeach
The problem is if I have a task with for example 2 tags. Then its going to loop two times and then I have two textfields with each a value of a single tag. I would like to have one single textfield with all the tags that a task has as a value. But I really don't know how to achieve this.
Can someone help me, please? Gladly I'm waiting for your response. Anyway thanks for your answer.
You can use #foreach inside the value attribute:
{{Form::label('tag', 'tags')}}
<input type="text" name="tag_name" class="form-control" value='#foreach ($task->tagtask as $tt) {{ $tt->tag['tag_name'] }} #endforeach'>
You can always use a little of pure PHP.
{{Form::label('tag', 'tags')}}
<input type="text" name="tag_name" class="form-control" value="<?php foreach($task->tagtask as $tt) echo $tt->tag['tag_name'];?>">
I have a loop of checkboxes with multiple attributes. One content may have many attributes, so user can check more than one attribute.
If some validation errors occurs in this form I need to pre-fetch already checked attribute.
#foreach($attributes as $entry)
<div class="check-line">
<input type="checkbox" id="cat4" class='icheck-me'
name="attribute[<?php echo $entry->id; ?>]"
data-skin="square"
data-color="blue"
value="{{$entry->id}}"
<?php
if(Input::old('attribute['.$entry->id.']')== $entry->id) {
echo 'checked="checked"';
}
?>
/>
<label class='inline' for="cat4">
<strong>{{$entry->name}}</strong>
</label>
</div>
#endforeach
I tried above but no luck.. any ideas?
From Laravel docs on Requests:
"When working on forms with "array" inputs, you may use dot notation to access the arrays:"
$input = Input::get('products.0.name');
So you should be able to do this fpr Input::old() as well:
<?php
if(Input::old('attribute.' . $ii) == $entry->id) {
echo 'checked="checked"';
}
?>
A slightly more catch-all answer to this would be to do an array search rather than just checking the current index. As such, I see the code looking like this:
#foreach($attributes as $entry)
<div class="check-line">
<input type="checkbox" id="cat4_{{{ $entry->id }}}" class="icheck-me"
name="attribute[]"
data-skin="square"
data-color="blue"
value="{{{ $entry->id }}}"
#if (in_array($entry->id, Input::old('attribute')))
checked
#endif
/>
<label class="inline" for="cat4_{{{ $entry->id }}}">
<strong>{{{ $entry->name }}}</strong>
</label>
</div>
#endforeach
So first off I've made the code more Blade-compliant:
Any echos are now {{{ and }}} rather than the previous mix of <?php echo ___; ?> and {{/}} (I went for {{{ rather than {{ as it's not HTML being echoed and it's better to be safer)
The <?php if () { is now a Blade #if ()
The name attribute is now just a standard array (doesn't include indices, as it doesn't need to), however the id attribute previously gave all checkboxes the same ID. While browsers will let you do this, it's technically illegal HTML, so I've appended the entry ID to each checkbox.
Finally, the if condition no longer checks that the value of the input with the current index matches the current entry's ID, but instead searches to see if the current entry's ID is anywhere in the returned array. This protects against the possibility of your entries being returned in a different order to the previous time they were on the page.
We now don't have a reliance on the $ii variable, so it can be removed too.
Caveats:
In doing this I've made the code a little nicer, but the code is no longer identical. Without knowing exactly why you use the $ii variable in order to provide keys to your attribute array I can't say that using my code will work correctly. However, assuming it was just there to help with this old input thing, then it's fine.
Also, my change of {{ to {{{ may have consequence I don't know about. I'd have thought for the $entry->id stuff it'd be fine, but maybe $entry->name in the <label> need to be unescaped HTML for a reason. Feel free to change this back.