Home > OS >  Pass parameters to a controller from the View by using ajax and controller pass the new eloquent to
Pass parameters to a controller from the View by using ajax and controller pass the new eloquent to

Time:10-13

This is how my Controller and View work

Controller

class DayhomeController extend Controller{
public function index(){
 $Dataset = Modal::all();
 return view('DayHome')->with('DataSet',$DataSet)
}

View

 <div  id="container1">
   <input type="date" id="datepicker" name="datepicker" value="<?php echo date('Y-m-d'); ?>"
  </div>
@for ($i = 0; $i < count($DataSet); $i  )
<div  id="container2">
    <div> {{$DataSet[$i]->name}} </div>
    <div> {{$DataSet[$i]->number}} </div>
</div>
@endfor

<script type="text/javascript">
    $('#datepicker').on('change', function() {
     var datepicker = $('#datepicker').val();
     alert(datepicker);

     $.ajax({
            url: '/orderdata',
            data: datepicker,
            type: "get",
            cache: false,
            success: function () {
                $('#something').empty();
                $('#something').append();
            },
            error: function () {
                ;}});});
</script>

Route

Route::get('/orderdata', 'DayhomeController@OrderDataorIndex');

1.I would like to ask if I use ajax to pass the datepicker value to the controller, should I pass it to the index or create another public function OrderData($Request $datepickerVal){} ? because I need to use the value of the datepicker as a condition to retrieve the modal update Dataset[i] again.

2.The data enters function index or function OrderData, and finally returns a new dataset[], will this help me refresh the page, or should I do something like $('#something').empty $('#something').append() in ajax success:function to update my object and its {{$DataSet[$i]->name}} {{$DataSet[$i]->number}} number?

CodePudding user response:

You have 3 ways to accomplish this

Page reload

Reload the page and send query string in the url eg: "/yourpath?date=" $date, then in your controller:

window.location.href = 'https://your.url/path?date='   $('#datepicker').val();
class DayhomeController extend Controller{
public function index(){
 $Dataset = Modal::all();
  if(request()->get('date')){
    $Dataset = $Dataset->where('date',request()->get('date'))
  }
 return view('DayHome')->with('DataSet',$DataSet)
}

Ajax return HTML

you could make a controller page/view that only returns the HTML of your rendered data set.

then you can call this route/controller and just replace the html with jQuery:

 $.ajax({
            url: '/orderdata',
            data: datepicker,
            type: "get",
            cache: false,
            success: function (htmlReceived) {
                $('#something').html(htmlReceived);

            },
       });})

Ajax javascript rendiner library

you could also always get the Data set via ajax and instead create your render method locally using, react, alpineJS, svelte, vue or any other frontend rendering library or framework

*** This code does not follow best practises but tries to explain your problem

  • Related