Access old("value") in form edit - php

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>

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.

How to return value from different cell of table column by foreign key id in Laravel 8.*

I made view that adds edits data, to database, its table got foreign keys. So for request I need its id. Can i somehow display name of it while under will be ids?
Im sorry if title is missleading, i dont know how to ask about this properly
In my controller im using both tables for foreign and master keys
Im doing it using this: (i dont know how its called)
Form code
<div class="form-group col-md-4">
<label for="inputState">City</label>
<select id="inputState" name="city_id" class="form-control">
<option selected >{{ $data->city_id }}</option>
#foreach($city as $city)
<option>{{ $city->id }}</option>
#endforeach
</select>
</div>
I think this is you're looking for. On edit, you pass the $data and $city properties to blade and on loop you can make selected the option in select element if the condition is checked
<div class="form-group col-md-4">
<label for="inputState">City</label>
<select id="inputState" name="city_id" class="form-control">
<option value="">Select</option>
#foreach($city as $city)
<option value="{{ $city->id }}" #if($city->id == $data->city_id) selected #endif>{{ $city->name }}</option>
#endforeach
</select>
</div>

How to select the option which is in `location_id` in `shops` table in laravel?

<div class="col-md-6">
<div class="form-group">
<label class="bmd-label-floating">Location</label>
<select value="{{ $shop->location_id }}" name="location" class="form-control">
#foreach ($locations as $location)
<option value="{{$location->id}}">{{$location->address}}</option>
#endforeach
</select>
</div>
</div>
I have two table. shops and locations. shops table have a foreign key named location_id which is the foreign key of locations table. Now I can update the shops location in shops table. And locations table have some rows.
The problem is when I update the shop->location_id. It updates. But when I update the shop again. Always selected the first one of locations table. But it should be selected the one which is actually the $shops->location_id How can I select the location which is in shops table?
You can you use condition in you foreach. {{ $shop->location_id == $location->id ? 'selected':'' }}
full code:
<div class="col-md-6">
<div class="form-group">
<label class="bmd-label-floating">Location</label>
<select value="{{ $shop->location_id }}" name="location" class="form-control">
#foreach ($locations as $location)
<option value="{{$location->id}}" {{ $shop->location_id == $location->id ? 'selected':'' }}>{{$location->address}}</option>
#endforeach
</select>
</div>
</div>
Putting value attribute to your select tag won't work. You need to compare your shop location id within the loop to add the selected attribute to the option tag
<select name="location" class="form-control">
#foreach ($locations as $location)
#if ($location->id == $shop->location_id)
<option value="{{$location->id}}" selected>{{$location->address}}</option>
#else
<option value="{{$location->id}}">{{$location->address}}</option>
#endif
#endforeach
</select>

Laravel Dropdown Form Get Form Database

I am using laravel 5.4 . I am trying to get data form Department table and data come also the problem there more dropdown menu come also please tell how can i solve this problem. Thanks advance.[
<div class="form-group">
{{Form::label('department','Department')}}
#foreach($department as $value)
{{Form::select('department',[$value->name], ' ', array('placeholder' => 'Select Department','class'=>'form-control'))}}
#endforeach
</div>
It's not entirely clear what you're after. Your current code seems to output several select boxes, one for each department and my best guess is that you want to output only one select box, with each department as an option in it. You can achieve that by doing something like this (You may have to adapt this code slightly to suit your needs):
The manual approach
<div class="form-group">
<label for="department">Department</label>
<select id="department" name="department" class="form-control">
<option value="">Select Department</option>
#foreach ($departments as $department)
<option value="{{ $department->id }}">{{ $department->name }}</option>
#endforeach
</select>
</div>
Using Laravel Collective's Form Builder
<div class="form-group">
{!! Form::label('department', 'Department') !!}
{!! Form::select('department', ['' => 'Select Department'] + $departments->pluck'name', 'id')) !!}
</div>
Try to do this way :-
<div class="form-group">
{{Form::label('department','Department')}}
<select class="form-control" name="department">
#foreach($department as $value)
<option value="$value">$value</option>
#endforeach
</select>
</div>

Alternative to calling models in the views - Laravel

Trying to improve the code of my app and just migrated to L5. I used to call models in my views and I know this is not best practice - I should separate data calling and views completely.
However how do you deal with a situation like this:
<div class="field">
<label for="country">Country<sup>*</sup></label>
<select class="" id="country" name="country">
<option value="{{{ old('country') ? old('country') : '' }}}">{{{ old('country') ? App\Country::find(old('country'))->country_name : '' }}}</option>
#foreach ($countries as $studio_country)
<option value="{{ e($studio_country->id) }}">{{ e($studio_country->country_name) }}</option>
#endforeach
</select>
#if ($errors->has('country'))
<div class="alert alert-warning" role="alert">
{{ $errors->first('country') }}
</div>
#endif
<br>
</div>
Basically if there is an input and the input did not pass the validation the page refreshes with the old input and the error message. I need to extract the name of the country from the DB since I only have its ID.
You would want to do something like this:
<select id="country" name="country">
#foreach ($countries as $studio_country)
<option
value="{{ $studio_country->id }}"
{{ $studio_country->id === old('country') ? 'selected' : '' }}>
{{ $studio_country->country_name }}
</option>
#endforeach
</select>

Categories