欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Laravel old for select selectbox options 有大用

Asked 
Viewed 7k times
0

I can't manage to repopulate a Select field input after a query, using Laravel 4:

// Route
Route::get('blog', 'BlogController@getPosts');


// BlogController
public function getPosts()
{
    $posts = Post::where('category_id', Input::get('category'))->paginate(25);

    $categories = Category::lists('title', 'id');

    return View::make('blog', compact('categories', 'posts'));
}

// Blog view    
{{ Form::open('method' => 'get', 'id' => 'form-search') }}

    {{ Form::select('category', $categories, Input::old('category')) }}

{{ Form::close() }}

I managed to make it work this way, but it's not the best practice

<select name="category" id="category">
    <option value="1" {{ (Input::get('category') == 1) ? 'selected="selected"' : null }}>Category 1</option>
    <option value="2" {{ (Input::get('category') == 2) ? 'selected="selected"' : null }}>Category 2</option>
    <option value="3" {{ (Input::get('category') == 3) ? 'selected="selected"' : null }}>Category 3</option>
</select>

I think the Input::old('category') doesn't work because it is a GET request, am I right? Is there any workarounds?

Update : I finally made it work using Input::get() instead of Input::old() :

{{ Form::select('category', $categories, Input::get('category')) }}
  • The 2nd argument to Input::old() is a default value in the event that old is empty. Input::old('category', null); for instance. – Ohgodwhy Sep 24 '14 at 20:39 
  • I guess so, but I'm 100% sure that my input is not null, since my posts listing works well, and my uri looks like /?category=1 – Phmarc Sep 24 '14 at 20:45

2 Answers    正确答案

2

Perhaps this will work

public function getPosts()
{
    $category_id = Input::get('category');

    $posts = Post::where('category_id', $category_id)->paginate(25);

    $categories = Category::lists('title', 'id');

    return View::make('blog', compact('categories', 'posts', 'category_id'));
}

// Blog view    
{{ Form::open('method' => 'get', 'id' => 'form-search') }}

    {{ Form::select('category', $categories, $category_id) }}

{{ Form::close() }}
  • That worked well, and then I tried simply using Input::get() instead of Input::old() and it worked too! Thanks – Phmarc Sep 24 '14 at 21:10
3

It seems you're not even retrieving the old input, you will need to pass it to the view. You can do that in one of two ways, the easiest and best to understand is to just specify that you want to pass input.

return View::make('blog', compact('categories', 'posts'))->withInput();

Also, you don't need the markup in your HTML. Laravel will do this for you if you just give it the value of the old Input. It works very well.

Your Answer

来自   https://stackoverflow.com/questions/26025811/repopulate-laravel-inputold-on-formselect




Asked 
Viewed 43 times
0

So i'm working on a standalone laravel project (still a bit new to this..). Using the same form blade file adding and editing records. I've set old to the fields for validation checks, however i can't seem to get old working correctly for dropdowns. The select options are loaded in a foreach, and ive got a check to set it to selected if the record on edit matches the dropdown option selected, however when i add the old method to it, select a new option (E.G. "broker") and force validation to throw an error it works as expected & saves whatever i've selected (option "broker"), it saves the option on submit, but then when i go to edit another record (option "assessor") it displays the same option i previously selected (option "broker")...if any of that made sense!

Here's the foreach with option select from the blade file:

<select class="form-control" name="other_party_details[party_type_id]" id="other_party_details[party_type_id]">
<option value="">--- Please Select ---</option>
@foreach($partyTypes as $partyType)
<option value="{{ $partyType->id }}" {{old('other_party_details.party_type_id', !empty($claim->otherParty->party_type_id)) == $partyType->id ? 'selected' : '' }}>
{{ $partyType->label }}
</option>
@endforeach
</select>

What am i doing wrong?

I've exhausted google as for the most it seems to show results for multi select (mine is not), or using LaravelCollective (which i'm not), so running out of places to look!

Thanks in advance

  • what's name of select tag? – Khushbu Feb 20 '20 at 9:32
  • i've added the whole select code to the original question. Other fields (regular inputs) are also named as such in the html array format and old works as expected – AisRuss Feb 20 '20 at 9:35

1 Answer    正确答案


1

You can try below code

<option value="{{$partyType->id}}" @if(old('party_type_id') == $partyType->id || $claim->otherParty->party_type_id == $partyType->id ) selected @endif> {{ $partyType->label }}</option>
  • Just replaced my option code with that and selected a different option on edit, but validation errors goes back to the original again instead of what was selected. – AisRuss Feb 20 '20 at 9:39
  • Is your select tag name dynamic? i mean party_type_id in other_party_details[party_type_id] is dynamic? – Khushbu Feb 20 '20 at 9:43 
  • You can change position of both conditions in if. Check edited answer – Khushbu Feb 20 '20 at 9:44
  • The select box isnt dynamic, it jut grabs all selections from the model and passes through to the blade file before being spat out in the foreach. swapping the conditions didnt change i'm afraid – AisRuss Feb 20 '20 at 9:49
  • It seems like there is no issue with your select tag code. Check your save method and make sure on saving correct rows and columns are updated. – Khushbu Feb 20 '20 at 9:53

Your Answer


来自 https://stackoverflow.com/questions/60316466/laravel-old-for-selectbox-options




普通分类: