-2

i am technically new to javascript and trying to pass a value to php script to use it in where clause...

Blade html

                    <div class="tab-content blog_tabs">
                            <div class="tab-pane" name="schedule" id="" >
                                <?php
                                    $tabSchedule = App\Schedule::where('route_id',  )   
                                                    ->latest()
                                                    ->get();
                                ?>
                                @foreach ($tabSchedule as $schedule)
                                    <ul class="list-group">
                                        <li class="list-group-item">{{ $schedule->schedule_number}}</li>
                                    </ul>
                                @endforeach    
                            </div>
                        </div>
                    </div>

javascript

$("ul.nav-tabs > li > a").click(function() {
        var id = $(this).attr("href").replace("#", "");
        console.log(id);

        if(id) {
            $.ajax({
                url: "{{ route('user.schedule.getId') }}",
                type: "GET",
                data:{'id':id},
                dataType: "json",
                success:function(data) {

                    var route_id = 1;
                    
                }
            });
        }    
    });

So here is a picture of what i want to pass:

enter image description here

As shown in the image I am trying to pass route_id to a Laravel PHP script to use it in a where clause.

karel
  • 5,489
  • 46
  • 45
  • 50
Lafoune
  • 436
  • 2
  • 12
  • 28

2 Answers2

0

What you trying to do is impossible, since php is a preprocessor, processes request before page loads, and JS starts processing onload. You already have the workaround and that is ajax.

The approach is simple:

  1. Load the page without the $tabSchedule variable.
  2. Use ajax to fetch the variable from backend and then use JS to render the $tabSchedule on blade. Laravel offers various tools for this, and of course you can still use vanilla JS or jquery if that's your thing.
  3. Use JS to apply filters by running an Ajax call to update the variable.
AntonyMN
  • 660
  • 6
  • 18
-1

you can't do that, if you have this value you need to pass it to your view or save it in cookies to get it when you need.

Mohammed Aktaa
  • 1,345
  • 9
  • 14