Store two values from Dropdown Option Laravel 5.7 - laravel

I am using dropdown and it's option value is $account->code but it shows code and name
so i want to store code in its column and name in another column
here is my blade code :
<div class="form-group col-md-4">
<label>#lang('site.from_account_number')</label>
<select class="form-control select2" style="width: 100%;" name="from_account_number" id="from_account_number">
#foreach ($accounts as $account)
<option value="{{ $account->code }}">
{{ $account->code }} - {{ $account->name }}
</option>
#endforeach
</select>
</div>
and here is my controller code :
$depositaction->from_account_number = $request->get('from_account_number');
$record_from_acc_name = Account::find($request->get('from_account_number'));
$depositaction->from_account_name = $record_from_acc_name->name;
but i get error said :
ErrorException (E_NOTICE) Trying to get property 'name' of non-object

You should query in code. Please check the below code.
$record_from_acc_name = Account::where('code', $request->get('from_account_number'))->first();

Related

show old data of `select` element in blade laravel 7

That's code showing deliverer names and if one selected I got his id on db table
<select name="deliverer_id" class="selectpicker form-control">
#foreach($dvs as $dv)
<option value="{{$dv->id}}"> {{ $dv->name }} </option>
#endforeach
</select>
I wanna show the old name of deliverer_id. I try before #if ...
You will need to compare your value in foreach loop with the old submitted value. For that Laravel provides a method to retreive old value from request $request->old('deliverer_id');
Or you can do it in your blade like this
<select name="deliverer_id" class="selectpicker form-control">
#foreach($dvs as $dv)
<option value="{{$dv->id}}" #if($dv->id == old('deliverer_id')) selected #endif > {{ $dv->name }} </option>
#endforeach
</select>
I foud the solution
#foreach($dvs as $dv)
<option value="{{ $dv->id}}"{{ old('deliverer_id', $order->deliverer_id) == $dv->id ? 'selected' : '' }} > {{$dv->name}}
</option>
#endforeach
I'm not sure what you mean but it seems that you want to select a default option based on specific condition in that case you can do as in the following:
<select name="deliverer_id" class="selectpicker form-control">
#foreach($dvs as $dv)
<option value="{{$dv->id}}" {{$dv->id == $old_deliverer_id ?? "selected" : ""}}> {{ $dv->name }}</option>
#endforeach
</select>

Get input value and pass it to route in same blade file Laravel 5.7

I want to pass selected value from dropdown to be passed throw routing from the same blade file
Here is my blade code :
<div class="form-group col-md-2">
<label>#lang('site.son')</label>
#if(Auth::user()->shortsign == '--')
<select class="select2-size-lg form-control border-primary" style="width: 100%;" name="sign">
#foreach ($users as $user)
<option value="{{ $user->shortsign }}">{{ $user->shortsign }}</option>
#endforeach
</select>
#else
<select class="select2-size-lg form-control border-primary" style="width: 100%;" name="sign" readonly>
<option value="{{ Auth::user()->shortsign }}">{{ Auth::user()->shortsign }}</option>
</select>
#endif
</div>
<div class="form-actions right">
<i class="la la-search" aria-hidden="true"></i> #lang('site.search')
</div>
Now the code iam using pass the last value from database not selected value
you can try this by giving id to your anchor tag
<script type="text/javascript">
$('.select2-size-lg').change(function () {
var id = $(this).val();
document.getElementById("abc").href="{{ url('/salereports/report/' . id) }}";
});
</script>

How to show default value in select option (parent_id)?

I tried to save a parent_id of Category in Laravel 5.8 successfully, but I want to edit category now.
CategoryController.php
public function edit(Category $category)
{
return view('Admin.categories.edit', compact('category'));
}
public function update(CategoryRequest $request, Category $category)
{
$category->update($request->all());
return redirect(route('categories.index'));
}
edit.blade.php
<form action="{{ route('categories.update', $category->id) }}" method="post">
{{ method_field('PATCH') }}
{{ csrf_field() }}
#include('Admin.layouts.errors')
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" value="{{ old('name') ? : $category->name }}" id="name" name="name">
</div>
<div class="form-group">
<label for="parent_id">Sub Category</label>
<select class="form-control" id="parent_id" name="parent_id" data-live-search="true">
#foreach(\App\Category::all() as $category)
<option value="{{ $category->id }}" {{ trim($category->id) , $category->pluck('id')->toArray() ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
For example, I have the following categories in the database.
id name parent_id
1 Software 0
2 Hardware 0
3 Photoshop 1
4 CoredDraw 1
If a user, for example, selects CorelDraw for edit, open edit for it. Select the name input tag, write CorelDraw. Select the parent_id option tag select Software. Because this is parent_id is = 0.
You need to do some basic improvement in your code, Make a practice of fire query in the model or in the controller
Blade file is not for load the data from the database
Controller
public function edit(Category $category)
{
$allCategory = \App\Category::all();
return view('Admin.categories.edit', compact('category','allCategory'));
}
Blade
<select class="form-control" id="parent_id" name="parent_id" data-live-search="true">
<option value="">Select Parent</option>
#foreach($allCategory as $cate)
<option value="{{ $cate->id }}" {{ $category->id == $cate->id ? 'selected' : '' }}>{{ $cate->name }}</option>
#endforeach
</select>
Once the user starts typing different category name you need to reset the select box using any client side script

how to convert select box html to form html collactive

Hi i have this in my controller
$listPatient = Patient::get();
return view('backend.consultations.create')->with([
'patient' => $this->patient,
'patient_id' =>$this->patient_id,
'listPatient' => $listPatient,
]);
and in my view i have
<div class="form-group">
<div class="col-md-2">
List clients
</div>
<div class="col-lg-10">
<select name="patient_id">
<option value="0">Veillier séléctionner un patien </option>
#foreach($listPatient as $key)
<option value="{{$key->id}}">{{$key->nom_patient}} {{$key->prenom_patient}}</option>
#endforeach
</select>
</div><!--col-lg-10-->
</div><!--form control-->
and it work fine,i want to use Form::select but it doesn't work can anyone help me please
In your controller you would want the following to fetch $listPatient
$listPatient = Patient::lists("nom_patient","id")->toArray();
and in your view you would want
{!! Form::select('patient_id', [0 => 'Veillier séléctionner un patien'] + $listPatient, null) !!}
a simple solution
enter code here : <div class ="form-group">
{{ Form::label('list_patient', trans('validation.attributes.backend.consultations.nom_patient'), ['class' => 'col-lg-2 control-label required']) }}
<div class="col-lg-10">
<select id="patient_id" name="patient_id" class="form-control select2 box-size" required="required" placeholder=" Non définie" >
<option value="">Non définie</option>
#foreach($listPatient as $key)
<option value ="{{$key->id}}">{{$key->nom_patient}} {{$key->prenom_patient}}</option>
#endforeach
</select>
</div><!--col-lg-3-->
</div><!--form control-->

Undefined offset: 2 in Laravel 5

I want to keep the values in multiselect however, it shows Undefined offset: 2. The codes is below for the blade file.
<select multiple="multiple" name="warehouseId[]" id="warehouse" class="form-control" style="width:100%;">
#if($warehouseData)
#foreach ($warehouseData as $key => $warehouse)
<option value="{{$warehouse->id}}" >{{$warehouse->name}} {{$adminUserWarehouseSelectedData[$key]->id}}</option>
#endforeach
#endif
</select>
Add an #isset directive
<select multiple="multiple" name="warehouseId[]" id="warehouse" class="form-control" style="width:100%;">
#if($warehouseData)
#foreach ($warehouseData as $key => $warehouse)
#isset($adminUserWarehouseSelectedData[$key]->id)
<option value="{{$warehouse->id}}">
{{$warehouse->name}} {{$adminUserWarehouseSelectedData[$key]->id}}
</option>
#endisset
#endforeach
#endif
</select>
I think the answer you have accepted here is not 100% right. You have multiple data in $adminUserWarehouseSelectedData coming from controllers. As you have used multiple select here I assumed that.
So this is the proper way to do it.
<select multiple="multiple" name="warehouseId[]" id="warehouse" class="form-control" style="width:100%;">
#if($warehouseData)
#foreach ($warehouseData as $key => $warehouse)
<option value="{{$warehouse->id}}" >{{$warehouse->name}} #foreach($adminUserWarehouseSelectedData as $data) $data->id #endforeach {{$warehouse->name}}</option>
#endforeach</option>
#endforeach
#endif
</select>
I know that you are not using this code but for other users correct answer is the most preferable answer.

Resources