how to fix this category select method - laravel

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.

Related

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>

Setting selected option in laravel from database

I have a problem here, I want to make an edit form, I want the province that appears in the edit form to be the province selected from the database, here my code
<select name="provinsi" id="provinsi" class="form-control">
<option value="">Pilih Provinsi</option>
#foreach ($province as $prov)
<option value="{{$prov->name}}">{{$prov->name}}</option>
#endforeach
</select>
here my controller code
public function ShowSantri(Request $request,$id)
{
$province = DB::table('provinces')->get();
$profiles = Profile::find($request->id);
return view('admin/modal/edit_santri',compact('profiles','province'));
You can try like this:
<select name="provinsi" id="provinsi" class="form-control">
<option value="">Pilih Provinsi</option>
#foreach ($province as $prov)
<option value="{{$prov->name}}"
#if ($profiles['provName'] == $prov->name)
selected
#endif>
{{$prov->name}}
</option>
#endforeach
</select>
Just I'm not sure what is name in your $profiles for province. But I think that is what you need.
Here and here are some examples.
Good luck!
Use or change this code based on your needs:
<option value="{{$prov->name}}" {{ $prov->name == $profiles->prov_id ? "selected" : "" }}>{{$prov->name}}</option>
I think the $prov->name should be $prov->id, if not, you can change $profiles->prov_id to $profiles->prov_name. It depends on your database structure.

Laravel 5 issue with select drop down

The code for the controller:
public function index()
{$items = all_list::where('list_code', 'ABC-U47')->get();
//return $items;
return view('partials.curlist', compact('items'));
I know this works, I tested it with the return $items statement.
The code for the blade view:
</div><div class="form-group">
<select class="form-control" name="item_id">
#foreach($items as $item)
<option value="{{ $item->list_data }}"></option>
#endforeach
</select>
</div>
The view just shows and empty box. I know this is something very simple but, for the life of me I can't get it to work. Any help would be appreciated. Thanks in advance.
You need to have a text value for your option tag. Like this:
<option value="{{ $item->list_data }}">{{ $item->list_data }}</option>

Select from dropdown always return first record?

I have a problem with dropdown, i always get first record. Any suggestion?
$category = $request->input('article_category');
<select class="form-control" name="article_category">
<option value="" disabled>--Please select article category</option>
#foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->title }}</option>
#endforeach
Please make the following change...
<select class="form-control" name="article_category">
<option value="" disabled>--Please select article category</option>
#foreach ($categories as $category)
<option value="{{ $category->id }}" #if(request()->input('article_category') == $category->id){{ 'selected' }}#endif>{{ $category->title }}</option>
#endforeach
</select>
Your $category = $request->input('article_category') was getting overwritten by the foreach($categories as $category) written in foreach.
Also, you must add the selected keyword to mark the option as selected on page load.

Resources