Laravel Select2 default value - php

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 ?

Related

how to Concatenate an id in a foreach loop using jquery laravel

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

Setting multiple Tag checkboxes as "Selected" from Array

Struggling with something simple!
I'm new to Laravel 8 and have a number of checkboxes which are dynamically created saving their values to an array. My problem occurs when i want to flag previously active options as "selected".
The problem appears to be with the in_array argument and where i'm creating the array..
#php
$currentTags = $prop->tags->pluck('id');
#endphp
#foreach($tags as $tag)
<div class="custom-control custom-switch">
<input type="checkbox"
#if(in_array($tag->id, $currentTags)) selected #endif
class="custom-control-input"
onclick="$(this).val(this.checked ? {{ $tag->id }} : 0)"
id="tags[{{ $tag->id }}]"
name="tags[{{ $tag->id }}]">
<label class="custom-control-label" for="tags[{{ $tag->id }}]">{{ $tag->name }}</label>
</div>
#endforeach
Error
TypeError
in_array(): Argument #2 ($haystack) must be of type array, Illuminate\Support\Collection
As #El_Vanja commented:
The error is rather clear - it expects an array, but you gave it an object of type Illuminate\Support\Collection. If you check out the documentation of that class, you'll see it has a method called all that retreives collection items as an array.
Also should change selected to checked.
change this
#if(in_array($tag->id, $currentTags)) selected #endif
To
#if(in_array($tag->id, $currentTags->toArray())) checked #endif
or
#if(in_array($tag->id, $currentTags->all())) checked #endif

Is it possible to compare Session::get('key') value and compare it using laravel inline if else statement?

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

Retrieving and passing data to form input

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 } }}">

Outputting old form data with Twig

Is there an easier way to output old form data with Twig in this example? Inside this ternary operator, it automatically escapes the value="" string which is undesired. I still want the old email value escaped.
<input type="text" name="email" id="email"{{ request.post('email') ? (' value="' ~ request.post('email')|e ~ '"')|raw : '' }}>
I'm not sure what request.post is, but assuming it behaves like Symfony's app.request.request.get (returning null if no such post param exists) you can simply do:
<input value="{{ request.post("email") }}" />
It'll be the value if it exists, or an empty value if it doesn't. If you are a purist and do not wish to see an empty value attribute, you can do this:
<input{% if request.post("email") %} value="{{ request.post("email") }}"{% endif %} />

Categories