how to make button in table to route to url

  html-table, jquery, laravel, php

I have table <thead> initialized in in blade and foreach loop for <tbody> in controller

so this is how it looks in controller:

foreach($data as $row)
       {
        $output .= '
        <tr>

         <td>'.$row->year.'</td>

         <td>
            <a href={{url(/edit/'.$row->id.')}} class="...">Edit</a>
            <a href="#" class="...">Delete</a>
         </td>

        </tr>
        ';
       }

and in the blade, initialized like this:

<table>
       <thead>

        <tr>
         <th>Year</th>
         <th> BUTTON </th>
        </tr>

       </thead>

       <tbody></tbody>

      </table>

and the script to fetch data into the <tbody>:

<script>
$(document).ready(function(){

 fetch_customer_data();

 function fetch_customer_data(query = '')
 {
  $.ajax({
   url:"{{ route('live_search.action') }}", // live_search: is the blade where the table is
   method:'GET',
   data:{query:query},
   dataType:'json',
   success:function(data)
   {
    $('tbody').html(data.table_data); // TABLE DATA
    $('#total_records').text(data.total_data);
   }
  })
 }

 $(document).on('keyup', '#search', function(){
  var query = $(this).val();
  fetch_customer_data(query);
 });
});
</script>

the data and the button appear in the table but the button does not work properly. there is a problem with how I’m using the button in the table, this button: <a href= "{{url(/edit/'.$row->student_id.')}}" class="...">Edit</a> generates this url: http://sms.test/%7B%7Burl(/edit/2)%7D%7D while the url should look like this: http://sms.test/edit/ID22 but when i change the href= "{{url(/edit/'.$row->student_id.')}}" to href= "#" it displays the # in the url bar. so i guess the problem is in this line "{{url(/edit/'.$row->student_id.')}}"

this is the route to edit:
Route::get('/edit/{id}', '[email protected]');

and the function edit @controller looks like this:

public function edit($id)
    {
      $student = Student::find($id); 
      $students = Student::all();
      return view('student',['students'=>$students,'student'=>$student,'layout'=>'edit']);

    }

I tried every method I know but could not get it to work:( please help

Source: Laravel

Leave a Reply