Home > Blockchain >  old() does not work on dependent dropdown in Laravel blade
old() does not work on dependent dropdown in Laravel blade

Time:12-04

I have two dropdown list in my Laravel page. I am trying to get old value of the dependent dropdown list (categorylist.blade) and make it selected once I fill and post the data. It returns back to the same page if the validation has not successfully completed. I am able to get all values except this one. I have tried Session::put() as well as Session::flash() but did not work. The category list is retrieved once you chose section as it requests the category list from the controller through ajax. How can I get the old value in the category dropdown list after the page refreshed.

Here is my section selection dropdown:

<select  name="section_id" id="section_id">
    <option value="">Choose Section</option>
    @foreach($sections as $section)
        <option value="{{$section['id']}}" @if(old('section_id') == $section['id']) selected @endif>{{$section['name']}} </option>
    @endforeach
</select>

My categorylist dropdown:

<label >Chose Category</label>
<div >
@include('admin.deal-management.categorylist')
</div>

And this is my categorylist view file:

<select  name="category_id" id="category_id">
    <option value="">Choose Category</option>
       @foreach($categories as $category)
                <option value="{{$category['id']}}" @if(old('category_id') == $category['id']) selected @endif>
                    {{$category['category_name']}}
                </option>
            @endforeach
      
</select>

and this is my main controller:

public function addEditDeals(DealAddEditRequest $request, $id=null){
//*** post starts here ***/
        if($request->isMethod('post')){
        $message = 'Updated successfully';
        $data=$request->all();
 
            $deal->fill($request->validated());
            $deal->save();

            return redirect()->back()->with($message);
        }

This is my categorylist controller:

public function findCategories(Request $request){
    if($request->json()){
        $data = $request->all();
        $categories = Category::where(['section_id' => $data['id'], 'status'=>1])->get()->toArray();
        return view('admin.deal-management.categorylist',compact('categories'));
 }
}

And finally, this is the jQuery part:

$(document).ready(function (){
    let sectionid = $('.sectionchoose').val();
    $.ajax({

        headers: {
            'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')
        },
        type: 'POST',
        datatype: 'json',
        url : '/admin/selectsection',
        data: {id:sectionid},

        success: function(response){
            $('.categorylist').html(response)
        }, error:function(){

        }
    })
})

CodePudding user response:

Finally, was able to find the solution after 12 hours. Whoever has the same issue can use the approach below:

Step 1: Send the Session value through the with() command:


                return redirect()->back()->withErrors($validator)
                    ->withInput()->with('cat_id',$data['category_id']);

            }

Step 2: Retreive the data in your main blade and attain hidden input:

<input  id="asd" type="hidden" value="{{Session::get('cat_id')}}"/>

Step 3: Get the retreived session value in Jquery:

$(document).ready(function (){
    let sectionid = $('.sectionchoose').val();
    let cat_id;
    if($('#asd').val()){
         cat_id = $('#asd').val();
    } else {
         cat_id = 0;
    }
    $.ajax({

        headers: {
            'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')
        },
        type: 'POST',
        datatype: 'json',
        url : '/admin/selectsection',
        data: {id:sectionid, cat_id:cat_id},

        success: function(response){
            $('.categorylist').html(response)
        }, error:function(){

        }
    })
})

Step 4: Send the session value to your dependent blade again (as cat_id here)

 public function findCategories(Request $request){
        if($request->json()){
            $data = $request->all();
            $cat_id = $data['cat_id'] ?? '';
            $categories = Category::where(['section_id' => $data['id'], 'status'=>1])->get()->toArray();
            return view('admin.deal-management.categorylist',compact('categories','cat_id'));

        }
    }

Done! There is not any other way to get old value of dependent dropdown list value so far. If somebody knows better way, please help to improve this answer.

  • Related