Working on edit with nested arrays in result (laravel 5.3) - php

I have a students_record table and i am saving my data in json format there
$students_data = Students::findOrFail($id);
$attributes = DB::table('studentsdata')->where('id', $student_data->id)->select('attributes')->first();
$result = json_decode($student_data->meta_attributes);
echo "<pre>";
print_r($result);
die;
I am getting result as
stdClass Object
(
[name] => test
[status] => Suscess
[country] => Array
(
[0] => Pakistan
[1] => USA
)
[days_time] => months
)
I passed result to view and i an getting first name simply as
<label> Name</label>
<div class="col-md-8">
<div class="input-icon right">
<input type="text" name="name" value="{{isset($result->name) ? $result->name : '' }}" />
</div>
</div>
Its giving my my name in that field fine now
i want to get countries array also in my country field All countries in that array please Help how can i get it here which i have in $result as i show input above against my query
<label> Country </label>
<div class="col-md-8">
<div class="input-icon right">
<select id="country" multiple name="country[]" onchange="
getValue('opens-clicks-region', 'student_tracking', 'region', 'country', this.value, this.id)" disabled="disabled">
<option value=""></option>
</select>
</div>
</div>

You can use a foreach loop to generate <option> tags, if you are using laravel blade its much easier
#foreach ($result->country as $country)
<option value="{{ $country }}"> {{ $country }} </option>
#endforeach
take a look at the blade documentation here : https://laravel.com/docs/5.3/blade#loops

Try some thing like this
<select id="country" multiple name="country[]" onchange="getValue('opens-clicks-region', 'student_tracking', 'region', 'country', this.value, this.id)" disabled="disabled">
#if(!empty($result->country))
#foreach($result->country as $key => $country)
<option value="{{ $key }}">{{ $country }}</option>
#endforeach
#else
<!-- Default case if there is no country -->
<option value="default_key">Default value</option>
#endif
</select>
FYI: Laravel blade has a check for isset input field
You can replace
<input type="text" name="name" value="{{isset($result->name) ? $result->name : '' }}" />
with
<input type="text" name="name" value="{{ $result->name or '' }}" />
Hope it helps!

Related

How to set the multiple default value in laravel livewire

i have a multiple product when i get the default value of that product, but when i get that value in input field the default value doesn't show ?
<x-filament::page>
<form wire:submit.prevent="submit">
<div>
#if (session()->has('message'))
<div class="alert alert-success">
{{ session('message') }}
</div>
#endif
</div>
<select wire:model="user_type">
<option value="">Select User Type.....</option>
<option value="admin">Admin</option>
<option value="distributor">Distributor</option>
<option value="retailer">Retailer</option>
</select><br><br>
<input wire:model="name" type="text" placeholder="Name"><br><br>
<input wire:model="email" type="email" placeholder="Email Address"><br><br>
<input wire:model="password" type="password" placeholder="Password"><br><br>
<div align = "center">Products</div>
** #foreach($product as $val)
<br><br> {{ $val->name }} :
<input type="number" wire:model="default_price" value = "{{$val->default_price}}">
#endforeach<br><br>**
<button type="submit">
Submit
</button>
</form>
</x-filament::page>
Check these screencasts. They are very helpful! https://laravel-livewire.com/screencasts/data-binding
<input type="number" wire:model="default_price" value = "{{$val->default_price}}">
Livewire doesn't use value attribute!
If you want to update Product->default_price, you can use https://laravel-livewire.com/docs/2.x/properties#binding-models or if you want to only output the default_price, then remove wire:model.
Also #foreach($products as $val) $products should be plural,
and $val should be $product, because it is a product variable. Naming is important, if you don't want to confuse yourself and others constantly.

Check checked checkbox in Laravel

I have this code:
$arrayWithSelectedValues = [[user_id] => 3,[language_id] => 2], [user_id] => 3,[language_id] => 12]]
And I have checkbox :
#foreach($languages as $language)
<fieldset>
<input type="checkbox"
class="icheckbox_square-red"
id="input-15" name="languages[]"
value="{{ $language->id }}">
<label for="input-15">{{ $language->name }}</label>
</fieldset>
#endforeach
How can I mark checkboxes as marked - those that have language_id from the array = $ language-> id?
Use the php function in_array() to check if the $language_id exists in the array of $languages
#foreach($languages as $language)
<fieldset>
<input type="checkbox"
class="icheckbox_square-red"
id="input-15" name="languages[]"
value="{{ $language->id }}"
#if(in_array($language_id, $languages))
checked
#endif
>
<label for="input-15">{{ $language->name }}</label>
</fieldset>
#endforeach
If $language->id value you are looking for, and you create an array of checked language ids from $arrayWithSelectedValues you can do:
<input
type="checkbox"
#if(in_array($language->id, $languages))
checked
#endif
>
You can collected the language IDs like so:
$languages = [];
foreach ($arrayWithSelectedValues as $selectedValues) {
$languages[] = $selectedValues['language_id'];
}

How can I fetch record drom database in edit view In Laravel?

In my edit view I have code like this
<div class="form-group">
<label class="col-md-12">Last Name</label>
<div class="col-md-12">
<input type="text" placeholder="Enter Last Name" name="lastName" class="form-control form-control-line" value="{{$profile->personal_detail['last_name']}}" required>
</div>
</div>
<div class="form-group">
<label class="col-md-12">Department</label>
<div class="col-md-12">
<select class="custom-select form-control col-md-11" id="department" name="department">{{ $profile->personal_profile['department'] }}
#foreach($listDepartment as $departmentList){
<option value='{{$departmentList->nameOfDepartment}}'>{{$departmentList->nameOfDepartment}}</option>
}
#endforeach
</select>
</div>
</div>
In edit view my last name field it gives me last name from database, in department it display me drop down of department but I want that inserted name of department in that field.
how can i get it??
I have other drop down like this
<div class="row">
<label class="col-md-6"><b> Mode </b></label>
<div class="col-md-6">
<select class="custom-select form-control col-md-12" name="mode" id="mode" required>
<option value=""> --- Select Interciew Mode --- </option>
<option value="telephonic">Telephonic</option>
<option value="facetoface">Face 2 face</option>
<option value="skype">Skype</option>
</select>
</div>
</div><hr>
This is my controller
public function candidateDetail($id)
{
$empDetails = User::all();
$candidateDetail = EmployeeHire::find($id);
$interview = [
'' => '--- Select Interciew Mode ---',
'telephonic' => 'Telephonic',
'facetoface' => 'Face 2 face',
'skype' => 'Skype'
];
return view('pages.candidatedetails', compact('id', 'candidateDetail', 'empDetails', 'interview'));
}
You can check in your foreach if the value of nameOfDepartment match the one from your user.
<div class="form-group">
<label class="col-md-12">Department</label>
<div class="col-md-12">
<select class="custom-select form-control col-md-11" id="department" name="department">
#foreach($listDepartment as $departmentList)
#if ($profile->personal_profile['department'] == $departmentList->nameOfDepartment)
<option value="{{$departmentList->nameOfDepartment}}" selected="selected">{{$departmentList->nameOfDepartment}}</option>
#else
<option value="{{$departmentList->nameOfDepartment}}">{{$departmentList->nameOfDepartment}}</option>
#endif
#endforeach
</select>
</div>
</div>
For your second select field, create an array with all possible values in your controller.
$interview = [
'' => '--- Select Interciew Mode ---',
'telephonic' => 'Telephonic',
'facetoface' => 'Face 2 face',
'skype' => 'Skype'
];
Then you can do the same as your previous select :
<select class="custom-select form-control col-md-12" name="mode" id="mode" required>
#foreach($interview as $key => $name)
#if ($profile->personal_profile['interview'] == $key)
<option value="{{ $key }}" selected="selected">{{ $name }}</option>
#else
<option value="{{ $key }}">{{ $name }}</option>
#endif
#endforeach
</select>

Can't send collection to bootstrap-select in Laravel form

Here is an edit form I've created
#extends('main')
#section('title', 'Edit Post')
#section('content')
<form class="form-group" action="/categories/update" method="POST">
{{csrf_field()}}
<input name="_method" type="hidden" value="PUT">
<label for="category_id" id="category_id">Select Category</label>
<select class="form-control" id="category_id" style="height:auto;" name="category_id">
#foreach($categories as $category)
<hr class="custom-hr-heading"/>
<option value="{{$category->id}}" {{($category->id == $post->category->id)?"selected='selected'":""}}>
{{$category->name}}
</option>
#endforeach
</select>
<label for="title" class="mt-2" name="title" id="title">Your Post Title</label>
<input type="text" class="form-control mb-2" value="{{$post->title}}" name="title" id="title">
<textarea class="form-control" name="body">{{$post->body}}</textarea>
<label for="tags" id="tags" name="tags">Select Tags..</label>
<select class="selectpicker mt-3" id="tags[]" name="tags[]" multiple="multiple">
#foreach($tags as$tag)
<option value="{{$tag->id}}">{{$tag->name}}</option>
#endforeach
</select>
<br/>
<button type="submit" class="btn mt-3 btn-success btn-sm">Update</button>
</form>
<script type="text/javascript">
tinymce.init({
selector: 'textarea',
theme: 'modern',
height: 300
});
$('.selectpicker').selectpicker({
style: 'btn-info'
});
</script>
#stop
Controller
public function edit($id)
{
$post = Post::find($id);
$categories = Category::orderBy('name', 'asc')->get();
$tags = Tag::orderBy('name', 'asc')->get();
return view('posts.edit', compact('post', 'categories', 'tags'))
}
I get a themed select box that just says nothing selected. Am I passing the tags collection to the select box correctly?
I'm using Laravel 5.5, PHP 7.2, and Bootstrap 4.
I've tried bootstrap-select via CDN and local files.
You are missing a space
Change:
#foreach($tags as$tag)
To:
#foreach($tags as $tag)
As per Bootstrap 4's documentation, this is a short attribute, not a name-value pair, so I would do it like so (and clean it up a little):
<select class="form-control" id="category_id" style="height:auto;" name="category_id">
<option value="">Choose an Option</option>
#foreach($categories as $category)
<hr class="custom-hr-heading"/>
<option value="{{ $category->id }}" #if($category->id == $post->category->id) selected #endif >
{{ $category->name }}
</option>
#endforeach
</select>
The problem was infact two problems.
Bootstrap 4 (final) support is only available in the latest bootstrap-select beta release here
Also my code was a little out. Here is the final working code.
<select class="selectpicker mt-3 form-control" name="tags[]" multiple>
#foreach($tags as $tag)
<option value="{{$tag->id}}" {{$post->tags->contains($tag->id)?"selected='selected":""}}>{{$tag->name}}</option>
#endforeach
</select>

Access old("value") in form edit

I'm currently working on an edit page. My edit perfectly shows the old (product name) input value, like so:
<div class="form-group">
<label for="product-input">#lang('product.name')</label>
<input id="product-input" type="text" name="name" class="form-control" value="{{ isset($product) ? $product->name : '' }}">
</div>
My question is, how can I do this with a select? How can I get the old selected option value when editing?
My code:
<div class="form-group">
<label>#lang('product.group')</label>
<select name="product_group_id" id="product_group_id" class="form-control" ng-model="productGroup" ng-change="changedType(val)">
<option style="display: none" value="">#lang('product.choose_group')</option>
#foreach ($product_groups as $product_group)
<option value="{{$product_group->id}}">{{$product_group->name}}</option>
#endforeach
</select>
</div>
Solution:
Added ng-selected to the option element, like so:
#foreach ($product_groups as $product_group)
<option value="{{$product_group->id}}" ng-selected="{{ isset($product->product_group_id) ? $product->product_group_id == $product_group->id : ''}}">{{$product_group->name}}</option>
#endforeach
You need to check if the old value equals option value and then use selected attribute:
#foreach ($product_groups as $product_group)
<option #if(old('product_group_id') == $product_group->id) selected #endif value="{{$product_group->id}}">{{$product_group->name}}</option>
#endforeach
Even better considering the first time with default value:
<option
value="{{$product_group->id}}"
{{ old('product_group_id', $product->id) != $product_group->id?: 'selected' }}>
{{$product_group->name}}
</option>

Categories