noblemfd
1 month ago
214
8
Laravel

Error:Undefined variable: rating_points in Laravel

Posted 1 month ago by noblemfd

In my Laravel-5.8 project, I have this codes:

Controller

public $rating_points = [
    "1" => "Three-Star-Rating",
    "2" => "Four-Star-Rating",
    "3" => "Five-Star-Rating",
    "4" => "Six-Star-Rating",
    "5" => "Seven-Star-Rating",
];

public function create()
{
    return view('appraisal.ratings.create')->with('rating_points', $this->rating_points);
}

public function store(StoreRatingRequest $request)
{
    try { 
            $input = Input::all();

            for ($idx = 0; $idx < count(Input::get('id')); $idx++)
            {
                $rating = new AppraisalRating;
                $rating->rating_point           =       $input['rating_point'][$idx];
                $rating->rating_type            =       $input['rating_type'][$idx];
                $rating->rating_value           =       $input['rating_value'][$idx];
                $rating->rating_description     =       $input['rating_description'][$idx];
                $rating->save();
            }
    
    Session::flash('success', 'Appraisal Rating is created successfully');
    return redirect()->route('appraisal.ratings.index');

    } catch (Exception $exception) {
            Session::flash('danger', 'Action failed!');
            return redirect()->route('appraisal.ratings.index');  
    }
}

create.blade

   <form  action="{{route('appraisal.ratings.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data">
       {{csrf_field()}}
                    <div class="form-body">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group row">
                                    <label for="rating-point" class="col-4 col-form-label"> Rating Point<span style="color:red;">*</span></label>
                                    <div class="col-md-6 controls">
                                        <select id='rating-point' class="form-control select2" data-placeholder="Choose Rating Value" tabindex="1" name="rating_point[]">
                                            <option value="">Select Rating Point</option>
                                            @foreach($rating_points as $k => $rating_point)
                                                <option value="{{$k}}" @if(old("rating_point") == "$k") selected @endif>{{$rating_point}}</option>
                                            @endforeach
                                        </select>
                                    </div>
                                </div>
                            </div>  
                        </div>

                    <div class="row" id="rating-data"></div>

        </div>
      
        <div id="rating-button" style="display: none;">
            &nbsp;&nbsp;&nbsp;<button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button>
            <button type="button" onclick="window.location.href='{{route('appraisal.ratings.index')}}'" class="btn btn-default">Cancel</button>
        </div>
    </form>

Javascript in the view blade

<script>
$(document).ready(function($){
$('#rating-point').on('change', function() {
  $('#rating-data').html('');

  if ( this.value == '1')
  {
    var numInputs = '3';
     for(var i=0; i < numInputs; i++)
     {
         var j = i*1+1;
         var $section =  $('<div class="col-md-4"><div class="form-group row"><label class="control-label text-right col-md-3">Type<span style="color:red;">*</span></label><div class="col-md-9 controls"><input  type="text" name="rating_type[]" value="'+j+'" class="form-control" value="{{old('rating_type')}}" readonly></div></div></div><div class="col-md-4"><div class="form-group row"><label class="control-label text-right col-md-3">Value<span style="color:red;">*</span></label><div class="col-md-9 controls"><input  type="number" name="rating_value[]" placeholder="Rating Value" class="form-control" value="{{old('rating_value')}}"></div></div></div><div class="col-md-4"><div class="form-group row"><label class="control-label text-right col-md-3">Description<span style="color:red;">*</span></label><div class="col-md-9 controls"><input  type="text" name="rating_description[]" placeholder="Enter rating description here" class="form-control" value="{{old('rating_description')}}"></div></div></div>');
         $('#rating-data').append($section);
     }           
    $("#rating-button").show();
  }
  else if ( this.value == '2')
  {
    //$('#rating-data').html('');
    var numInputs = '4';
     for(var i=0; i < numInputs; i++)
     {
         var j = i*1+1;
         var $section =  $('<div class="col-md-4"><div class="form-group row"><label class="control-label text-right col-md-3">Type<span style="color:red;">*</span></label><div class="col-md-9 controls"><input  type="text" name="rating_type[]" value="'+j+'" class="form-control" value="{{old('rating_type')}}" readonly></div></div></div><div class="col-md-4"><div class="form-group row"><label class="control-label text-right col-md-3">Value<span style="color:red;">*</span></label><div class="col-md-9 controls"><input  type="number" name="rating_value[]" placeholder="Rating Value" class="form-control" value="{{old('rating_value')}}"></div></div></div><div class="col-md-4"><div class="form-group row"><label class="control-label text-right col-md-3">Description<span style="color:red;">*</span></label><div class="col-md-9 controls"><input  type="text" name="rating_description[]" placeholder="Enter rating description here" class="form-control" value="{{old('rating_description')}}"></div></div></div>');
         $('#rating-data').append($section);
     } 
    $("#rating-button").show();
  }
  else if ( this.value == '3')
  {
   // $('#rating-data').html('');
    var numInputs = '5';
     for(var i=0; i < numInputs; i++)
     {
         var j = i*1+1;
         var $section =  $('<div class="col-md-4"><div class="form-group row"><label class="control-label text-right col-md-3">Type<span style="color:red;">*</span></label><div class="col-md-9 controls"><input  type="text" name="rating_type[]" value="'+j+'" class="form-control" value="{{old('rating_type')}}" readonly></div></div></div><div class="col-md-4"><div class="form-group row"><label class="control-label text-right col-md-3">Value<span style="color:red;">*</span></label><div class="col-md-9 controls"><input  type="number" name="rating_value[]" placeholder="Rating Value" class="form-control" value="{{old('rating_value')}}"></div></div></div><div class="col-md-4"><div class="form-group row"><label class="control-label text-right col-md-3">Description<span style="color:red;">*</span></label><div class="col-md-9 controls"><input  type="text" name="rating_description[]" placeholder="Enter rating description here" class="form-control" value="{{old('rating_description')}}"></div></div></div>');
         $('#rating-data').append($section);
     } 
    $("#rating-button").show();
  }
  else if ( this.value == '4')
  {
   // $('#rating-data').html('');
    var numInputs = '6';
     for(var i=0; i < numInputs; i++)
     {
         var j = i*1+1;
         var $section =  $('<div class="col-md-4"><div class="form-group row"><label class="control-label text-right col-md-3">Type<span style="color:red;">*</span></label><div class="col-md-9 controls"><input  type="text" name="rating_type[]" value="'+j+'" class="form-control" value="{{old('rating_type')}}" readonly></div></div></div><div class="col-md-4"><div class="form-group row"><label class="control-label text-right col-md-3">Value<span style="color:red;">*</span></label><div class="col-md-9 controls"><input  type="number" name="rating_value[]" placeholder="Rating Value" class="form-control" value="{{old('rating_value')}}"></div></div></div><div class="col-md-4"><div class="form-group row"><label class="control-label text-right col-md-3">Description<span style="color:red;">*</span></label><div class="col-md-9 controls"><input  type="text" name="rating_description[]" placeholder="Enter rating description here" class="form-control" value="{{old('rating_description')}}"></div></div></div>');
         $('#rating-data').append($section);
     } 
    $("#rating-button").show();
  }
  else if ( this.value == '5')
  {
   // $('#rating-data').html('');
    var numInputs = '7';
     for(var i=0; i < numInputs; i++)
     {
         var j = i*1+1;
         var $section =  $('<div class="col-md-4"><div class="form-group row"><label class="control-label text-right col-md-3">Type<span style="color:red;">*</span></label><div class="col-md-9 controls"><input  type="text" name="rating_type[]" value="'+j+'" class="form-control" value="{{old('rating_type')}}" readonly></div></div></div><div class="col-md-4"><div class="form-group row"><label class="control-label text-right col-md-3">Value<span style="color:red;">*</span></label><div class="col-md-9 controls"><input  type="number" name="rating_value[]" placeholder="Rating Value" class="form-control" value="{{old('rating_value')}}"></div></div></div><div class="col-md-4"><div class="form-group row"><label class="control-label text-right col-md-3">Description<span style="color:red;">*</span></label><div class="col-md-9 controls"><input  type="text" name="rating_description[]" placeholder="Enter rating description here" class="form-control" value="{{old('rating_description')}}"></div></div></div>');
         $('#rating-data').append($section);
     } 
    $("#rating-button").show();
  }
  else
  {
    $("#rating-button").hide();
  }
});
});
</script>

Basically, the select option on change is to load other form inputs and the on submit button everything is saved in the database.

The issue is that when I load the view blade (create.blade) I got this error:

ErrorException (E_ERROR) Undefined variable: rating_points (View: C:\xampp\htdocs\tsl-hris\resources\views\appraisal\ratings\create.blade.php)

How do I resolve this error (Undefined variable: rating_points)?

Thank you.

Please sign in or create an account to participate in this conversation.