0

I need some help. I want to create a delete function on the frontend page, that When clicked, it will delete that row on the table. i have try it his try and nothing work for me. Here is my code

 function onDelete(){

         $model = Accounts::where('id', $id)->first()->delete();

 }     
    ```
      


here is the  twig

            
      
                     <tbody>
                             {% for log in logs %}
                                    <tr>
                                        <td>{{log.name}}</td>
                                        <td>{{log.account_no}}</td>
                                        <td>{{log.code}}</td>
                                       <td>  
                                            <button data-request="onDelete"
                                               data-request-data="id:{{ log.id }}"
                                                data-request-confirm="Are you sure ?"
                                               class="btn btn-sm btn-default">
                                                     delete
                                            </button> 
                                       </td>
                                        
                                    </tr>
                                    
                                  {% endfor %}
                            </tbody> 
kams
  • 45
  • 1
  • 12
  • so is it not deleting row from database or its not reflating changes in UI. means if you refresh page is it deleted :) ? – Hardik Satasiya Aug 12 '20 at 07:19
  • both of them. nothing works when i click the button. – kams Aug 12 '20 at 09:30
  • any errors etc .. ? network tab log or screenshot, so it will help us to debug issue – Hardik Satasiya Aug 12 '20 at 09:37
  • When i remove this line ( data-request-data = "id:{{log.id}}"), i get this error "call to a member function delete on null". – kams Aug 14 '20 at 08:30
  • hmm if you remove that line and when you click `Accounts::where('id', $id)->first()->delete();` here you will not get `$id` so it will throw error. – Hardik Satasiya Aug 14 '20 at 08:44
  • here is the error it shows with the data-request-data = "id:{{log.id}}". error---- "Uncaught Error: Error parsing the data-request-data attribute value. SyntaxError: Unexpected token '}'. " – kams Aug 14 '20 at 09:15
  • When change that line to this code ( data-request-data="{{ log.id }}" ), i get this error "call to a member function delete on null". – kams Aug 14 '20 at 09:17
  • but i am not sure why you want to change it and what is problem with this `"id:{{log.id}}"` <- as this syntax is correct, I am not sure what you try to do with just `data-request-data="{{ log.id }}"` <- this is wrong format, so it can not send ID to server and it shows error. main thing is your question and your comments not describing what you want and why.. can you be more clear – Hardik Satasiya Aug 14 '20 at 12:31
  • i know the syntax is correct but the error was pointing at it ( data-request-data = "id:{{log.id}}") thats why i was doing some changes on it to figure out whats wrong. – kams Aug 19 '20 at 04:11

1 Answers1

0

i solved it. In my onDelete function i should use this " post('id')" instead of "$id".

 function onDelete(){

         $model = Accounts::where('id', post('id'))->first()->delete();

 }
kams
  • 45
  • 1
  • 12