laravel pass select option value to form action parameter - laravel

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.

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;

Trying to get property 'level' of non-object when trying to edit data and showing it in select option

I wanted to edit data and show the data in select option.
Heres my controller code:
public function edit($id)
{
$user = User::findOrFail($id);
return view('user.edit', compact('user'));
}
I'm trying like this but getting an error
<select class="form-control" name="level" id="level">
<option value="{{ $user->level }}"> {{ $user->level }} </option>
#foreach ($user as $item)
<option value="{{ $item->level }}">{{ $item->level }}</option>
#endforeach
</select>
WHat you're trying to do is loop a foreach on a single object, which is not correct.
if you see your controller, User::findOrFail($id) does not return a collection of objects, it returns only 1 object, and in your view, you're trying to loop thru that single object

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

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

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.

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>

Resources