Laravel how to selected option value in loop when it will change? - laravel

I am new in laravel.
I have written below code where category in a select box, process is not ajax. After sent get request how I will selected new value ?
<select name="company_category_id" class="form-control" id='category'>
<option value="0">--Select Category--</option>
#foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}
</option>
#endforeach
</select>
I have written javascript to sent get request in url
<script>
$(document).ready(function() {
$('#category').change(function() {
url = window.location.href.split('?')[0];
window.location.href = url + '?id=' + $(this).val();
});
});
</script>

I suppose that you need preselected value once you submit form with above code. If so,
You just need to put condition and if match then attribute selected will be placed. Something like this,
<option value="{{ $category->id }}" $selectedId == $category->id ? 'selected' : ''>{{ $category->name }} </option>
$selectedId will be passed from controller to view
If I missed something please clarify your question

Related

How to display a select only when other select's option was chosen?-

I have 2 selects on a page and i want to show the 2nd only when in the first select a specific option was chosen, is that possible in laravel? here the code:
<select class="form-select my-2" name="category_id">
<option disabled selected>Seleziona una categoria</option>
#foreach ($categories as $category)
<option value="{{ $category->id }}" #if ($category->id == old('category_id')) selected #endif>
{{ $category->name }}
</option>
#endforeach
</select>
<select class="form-select my-2" name="type_id">
<option disabled selected>Seleziona un tipo di sushi</option>
#foreach ($types as $type)
<option value="{{ $type->id }}" #if ($type->id == old('type_id')) selected #endif>
{{ $type->name }}
</option>
#endforeach
</select>
Using JQuery :
<script>
$('select[name="category_id"]').change(function (){
let categoryId = $(this).val();
if (categoryId == null)
{
$('select[name="type_id"]').hide();
}else{
$('select[name="type_id"]').hide();
}
});
</script>

selected option laravel livewire

I have a dropdown and in edit page I want to show old input. I use this code but it still shows "please select". Where is the problem?
<select wire:model.defer="form.department" id="form.department" class="tf-input">
<option value="null" selected disabled>{{ __('Please select') }}</option>
#foreach($this->departments as $department)
<option {{ $form->department_id == $department->id ? 'selected' : '' }} value="{{ $department->id }}" >{{ $department->name }}</option>
#endforeach
</select>
Livewire does not listen to selected attributes in HTML when you are using wire:model, because it will overwrite it with the value from the component. Therefor, remove it entirely.
<select wire:model.defer="form.department" id="form.department" class="tf-input">
<option value="null" disabled>{{ __('Please select') }}</option>
#foreach ($this->departments as $department)
<option value="{{ $department->id }}" wire:key="department-{{ $department->id }}">{{ $department->name }}</option>
#endforeach
</select>
As you can see, I removed the parts that adds selected, but I also added a wire:key to the options - this is because the elements are rendered in a loop, and will help Livewire keep track of all elements on a page.
To set a value that is bound through wire:model, you need to set the value in the component. So in the mount() method, you can do
$this->form['department'] = $department->id;

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>

laravel pass select option value to form action parameter

I am making a page with select option.
If I select one option and click a button. It will go to that page with parameter in url.
I want to know is there any easy way to insert categoryId as parameter into action?
<form method="GET" action="{{ route('routename', ['categoryId' => 1]) }}">
#foreach($categories as $category)
<select>
<option value="1">{{ $category->name }}</option>
<option value="2">{{ $category->name }}</option>
<option value="3">{{ $category->name }}</option>
</select>
#endforeach
<button>Change</button>
</form>
You are being too specific in your route
You should remove the categoryId from the route. Instead you should point your route to a controller that just accepts a Request. then you can read the value of the select box in the controller and direct the user to the view depending on the selected option.
Your view:
<form method="GET" action="{{ route('routename') }}">
#csrf
<select name='selectOption'>
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
</select>
#endforeach
<button>Change</button>
</form>
Your Route:
Route::get('url', 'Controller#functionName')->name('routename');
Your Controller:
use Illuminate\Http\Request;
public function functionName(Request $request)
{
$option = $request->selectOption;
if($option == 1){
CODE FOR OPTION 1 HERE
}
}
This way the route will not require the parameter to be defined in the URL. Instead you can pass it as a parameter in the request to be read in the controller. You could then have a if rule for each option that then returns a different view.

how to fix this category select method

Hi I'm just trying to make category for my movie blog and its working fine, but the editing category must be selected right.
So I did something like this:
//fetching all categories
#foreach($categories as $category)
//start option
<option
//fetching current movie categories
#foreach($movie->categories as $cat)
//matching is this category match with
#if($category->name === $cat->name)
//if match selected method works
selected="selected"
#endif
#endforeach
>{{ $category->name }}
</option>
#endforeach
and its working fine but i thinks its a wrong way
can u suggest me good way to do this
thanks
check this:
#if(in_array( $category->name ,$movie->categories->pluck('name'))
{{'selected' }}
#endif
if give you a type arg2 must be an array error. add a toArray() after the pluck methods.
Thats how I do it.
<select class="form-control" name="category_id">
#foreach($categories as $category)
<option value="{{$category->id}}" {{ isset($gallery) && $category->id == $gallery->category_id?'selected':''}}>{{$category->name}}</option>
#endforeach
</select>
You can use ternary operator instead of using if().
If you have many to many relationship then you can try
<select class="form-control" name="category_id">
#foreach($categories as $category)
<option value="{{$category->id}}" {{$movie->categories->contains('name', $category->name )?'selected':'' #endif >{{$category->name}}</option>
#endforeach
</select>
Hope this helps.

Resources