How do I return an option saved to the Laravel database? - laravel-5.8

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

Related

Laravel 8 multi value select filtering

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.

Livewire ignore does not receive correct data

I'm using selectize.js for dropdown styling with livewire. I'm using livewire for a data table with sortable columns and pagination. The issue is, every time I make pagination or a sort by column, the javascript goes missing thus, there's no styling for the dropdown. I've solved the styling issue using wire:ignore. Now the new problem that I have is that the data passed to the dropdown is not accurate.
#foreach($applications as $application)
<p>{{$application->status}}</p>
<div wire:ignore>
<select class="selectize" name="status" data-width="200px">
#foreach(['Pending',
'Hired',
'Under consideration'] as $status)
<option
#if($application->status === $status) selected #endif>{{ $status }}</option>
#endforeach
</select>
</div>
#endforeach
Inside the <p>{{$application->status}}</p> tag, I get the status 'Pending' but on the dropdown, it shows 'Hired'. The correct status is 'Pending'.
(from comment) #stalwart1014 for example, when I use "select2" I do this
in content section
<select id="select2" wire:model="some">
//.....
</select>
in script section
$(document).ready(function() {
window.initSelectDrop=()=>{
$('#select2').select2({
placeholder: '{{ __('Select') }}',
allowClear: true});
}
initSelectDrop();
window.livewire.on('select2',()=>{
initSelectDrop();
});
});
and in component
public function hydrate()
{
$this->emit('select2');
}
This not always function properly with JS element...but hope this can help you. Greetings
it's maybe a bit late, but worth to mention for the future, I will do not point directly to your issue but explain the workaround of wire:ignore, the wire:ignore directive ignores the updates or changes for the further requests and updates, this attributes is defined for playing with JS third party library, the wire:ignore directive prevent all it's nested elements from updating if you wish to except Childs from updating you can use wire:ignore.self directive

Problem accessing data with relations in Laravel

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

How to show selected checkbox in multiselect checkbox list in laravel?

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>

Display Object list from server in a Select option in Vue

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>

Resources