I want to create a multiple select option based on a parent select option. Here is what I have done to achieve this.
On my livewire php file:
public function updatedSelectedMajor($major_id) {
$this->skills = \App\Models\Major::where('major_id',$major_id)->with('mySkills')->get();
}
When I run this with this statement:
#foreach($skills as $skill)
<option value="$skill->mySkills">{{$skill->mySkills}}</option>
#endforeach
I get these results.
[{"skills_id":5,"name":"Digital Marketing","points":10,"created_at":null,"updated_at":null,"pivot":{"major_id":2,"skills_id":5}}]
With this I want to get only the skills_id as the value and name as the option name, but I'm getting error Property [name] does not exist. I tried with pivot, but it doesn't work either.
and when I do this:
<option value="$skill->mySkills[0]->skills_id">{{$skill->mySkills[0]->name}}</option>
it works fine, but I need to get all of the records, not just one.
Blade provides simple directives for working with PHP's loop structures. This directive functions identically to their PHP counterpart foreach, for example:
#foreach($skills->mySkills as $skill)
<option value="{{ $skill->id }}">{{ $skill->name }}</option>
#endforeach
join table
use \App\Models\Major;
public function updatedSelectedMajor($major_id)
{
$myvalue = $this->skills::join('major','major.id','major_id')
->where('major_id',$major_id)
->select('skills.*','major.myskills as myskills');
return $myvalue;
}
in blade
#foreach($skills as $item)
<option value="$item->skills_id">{{$item->mySkills}}</option>
#endforeach
Related
Good day, I am passing information from one component to another from a SELECT, when I do not load the information from the json file it works, but when I extract from the json I have the following error.
ErrorException
Trying to get property 'numero_prestamo_original' of non-object (View: C:\laragon\www\prestamos-cth\resources\views\livewire\prestamos\prestamos.blade.php)
The select field:
<option value="2022006799">2022006799</option>
#foreach ($prestamos as $prestamo => $valor)
<option value="{{ $valor->numero_prestamo_original }}">{{ $valor->numero_prestamo_original }}
</option>
#endforeach
Do you have any idea what is happening. thank you very much.
I have seen on the internet to make the change from -> to [ ] but it doesn't work either.
Thank you all for your help, the solution I found for that problem was to do the search with the required fields in the array and only print what is needed. Thanks for your help.
I'm trying to build Laravel project that will have a multi-select dropdown with list of categories. Selecting category should reload the page and apply filter.
"scopeFilter" in my model is actually doing filtering, but here i just need to find a way to properly form URL. The problem is that this code i have:
<form id="cats-form" action="#" method="GET">
<select multiple class="chosen-select" name="test[]">
#foreach($categories->get() as $cat) //this loop goes through existing categories in the system
#php
//if category is part of URL, pre-select it in the select:
$arr = request()->all();
$selected = '';
if(array_key_exists('test', $arr)) {
$testArr = $arr['test'];
$selected = in_array($cat->id, explode(',', $testArr)) ? 'selected' : '';
}
#endphp
<option {{ $selected }} value="{{ $cat->id }}">{{ $cat->name }}</option>
#endforeach
</select>
</form>
<script>
$(".chosen-select").chosen({ })
$('.chosen-select').on('change', function(evt, params) {
$('#cats-form').submit();
});
</script>
Is actually giving me this URL:
http://localhost:11089/?test%5B%5D=19&test%5B%5D=5
While i actually need this:
http://localhost:11089/?test=19,5
I guess it's a trivial problem to solve for someone with better knowledge of Laravel, can you tell me pls what i'm doing wrong here?
This is rather how the language or framework or library reads an URL query string. Assuming that your prefered way of URL string is a valid format (as I usually see the format ?arr[]=val1&arr[]=val2 and never see the second format that you preferred), both query string formats should be acceptable to PHP.
As Yarin stated that the best way PHP reads an array is the first method, you don't have to worry too much because the decoded URL query string is exactly in the first format.
I am programming a system in Laravel 5.8 and nenessito do the editing of the user through a select but when I make the request does not appear the option that the user selected.
I already tried to pass the Controller attractive but not right.
public function edit($id)
{
$users = User::find($id);
$institutions = Institution::all('razaosocial');
return view('users.edit', compact(array('users','institutions')));
}
<div class="form-row">
<div class="col-md-6">
{{Form::label('Instituição:')}}
<select class=form-control name="instituicao" id="instituicao">
<option value="null">Selecione uma Instituição</option>
#foreach($institutions as $institution)
<option value="{{$institution->razaosocial}}">
{{ $institution->razaosocial}}
</option>
#endforeach
</select>
</div>
</div>
I hope that appears in the view the option selects by during registration.
Hoping something works in programming is very frustrating and I am at the moment. compact() actually creates an associative array whose keys are variable names and their corresponding values are array values. So instead of
compact(array('users','institutions'));
make it
compact('users','institutions');
Here is a reference that solves this problem
Laravel: Display data in DropDown from database
I have a multi-select checkbox list. I want to show stored value in list using checkbox selected.
User informations are stored in Partner_Prefence table and user religion column named as p_religion
$profile_data= DB::table('partner_prefence')->select('p_religion')->first();
Fetching religions from religions table
$religion_data=DB::table('religion')->select('religion_id','religion_name')->orderby('religion_name')->get();
Multiselect list
<select multiple="multiple" name="religion[]">
#foreach($religion_data as $religion)
<option value="{{$religion->religion_id}}" {{$profile_data->p_religion == $religion->religion_id ? 'selected' : ''}}>{{$religion->religion_name}}</option>
#endforeach
</select>
I'm having trouble with showing which religions user have
{{$profile_data->p_religion == $religion->religion_id ? 'selected' : ''}}
as I understand you have multi select form, so you need show selected multiple column..
You're storing ids as a string but it's hard check that certain number in string. İf you convert string into a array, you can easly check with in_array() method. This method will return true if given value exist in given array
<select multiple="multiple" name="religion[]">
{{-- no need to explode every time, it will reduce your performance --}}
#php($religions = explode(',', $profile_data->p_religion))
#foreach($religion_data as $religion)
<option
value="{{$religion->religion_id}}"
{{--if user religion id exist in religions then mark as selected--}}
{{in_array($religion->religion_id,$religions) ? "selected" : ""}}>
{{$religion->religion_name}}
</option>
#endforeach
</select>
Is the p_religion column saving multiple IDs if it is a multi-select list? Would using in_array() work then instead of using $profile_data->p_religion == $religion->religion_id.
in_array ($religion->religion_id, explode(',', $profile_data->p_religion))
Added the explode() call on the off chance you are storing an imploded array.
You could also try and use the blade syntax for an if statement inline to see if it displays differently.
<select multiple="multiple" name="religion[]">
#foreach($religion_data as $religion)
<option value="{{$religion->religion_id}}" #if($profile_data->p_religion == $religion->religion_id) selected #endif>
{{$religion->religion_name}}
</option>
#endforeach
</select>
I receive a list of Grades that are in the right format ( $key => $value) in a $grades variable
How can I fill a select option input with Vue using this variable.
I guess I must bind the vue variable with : but I'm just beginning with vue and I can find so very little basic examples,
<select v-model="grades" class="form-control">
<option v-for="gradeValue in gradeValues" :gradeValues="{{ $grades /* Laravel Variable */ }}">#{{ gradeValue }}</option>
</select>
EDIT: I could make an ajax call from Vue, this should not be so complicated, but as the page load, my Laravel controller passes variables to my View, so, this is for me a cleaner aproach, in this case there is no need for ajax.
I think this has been over complicated for you, your code in this question looks close. Is your Vue component in the same file as your blade? Then it's just:
html
<select v-model="selectedGrade" class="form-control">
<option v-for="(grade, val) in grades" :value="val">#{{ grade }}</option>
</select>
js:
new Vue({
...
data:function(){
return {
grades:{{ $grades }},
selectedGrade:2 //some default value
}
}
...
});
If your component is in a separate file you still don't need a separate component for each select dropdown, you just need to pass all the required info into one component. Without a better understanding of your app, I'd guess it could be something like this:
<person-data :grades="{{ $grades }}" :categories="{{ $categories }}" :ages="{{ $ages }}"></person-data>
then the vue component:
var PersonData = Vue.extend({
props:['grades','categories','ages']
});
You have two choices:
You can create the option elements using blade's foreach method:
#foreach ($grades as $grade)
<option ...>{{ $grade }}</option>
#endforeach
Or you can create a dedicated component for your grades with your template and pass in the $grades variable. For example:
<grades :grades="{{ $grades }}"></grades>