How to delete a record from a table with Laravel?

0

In my application users logged in through a button register themselves with an activity, when they click on the button, the user and activity information is sent to a table in the database.

The above works in the following way:

Laravel driver:

public function eventoRegistrado(Request $request){
    $eventoRegistrado = new Registration();
    $eventoRegistrado->user_id          = $request->user_id;
    $eventoRegistrado->event_id         = $request->event_id;
    $eventoRegistrado->nombre_asistente = $request->nombre_asistente;
    $eventoRegistrado->nombre_evento    = $request->nombre_evento;
    $eventoRegistrado->save();   
}

Javascript function:

$http.post("/eventoRegistrado", {user_id: $scope.user_id, event_id: $scope.evento_id, 
                        nombre_asistente: $scope.nombre_asistente, nombre_evento: $scope.nombre_evento})

view:

 <button class="btn btn-primary" ng-click='registrarEvento("{{$event->name}}", "{{$event->id}}", "{{Auth::user()->id}}", "{{Auth::user()->name}}")'>Registrarme en este evento.</button>

The idea is that in addition to the registration button, there is also a button to delete the record, but I do not know how to create a function to eliminate the record already done.

    
asked by Talked 08.10.2018 в 18:22
source

1 answer

1

It would be to create a route in your Laravel application, according to the documentation about HTTP a set of request methods is defined link for this case would be a DELETE (delete a specific resource) you should register the new route, in the directory /app/Http of your Laravel project you will find the file routes.php . You must add the following line remember to put the route, the controller and the function according to your consideration.

Route::delete('/api/v1/eventos/{id}', 'Eventos@destroy');

We create a function in the controller that will take care of receiving the id and eliminate it by returning the success message to Angular

public function destroy(Request $request) {
    $evento = Registration::find($request->input('id'));
    $evento->delete();
    return "evento eliminado con exito";
}

In your view ... taking into account that you already did the GET of the list of events and we show it in a table

<tbody>
    <tr ng-repeat="x in eventos">
        <td>{{x.id }}</td>
        <td>{{x.name }}</td>
        <td>
            <button class="btn btn-danger" ng-click="btnEliminar(x.id)">Delete</button>
        </td>
    </tr>
</tbody>

In your controller of your project Angular creates the delete function, which receives the id to be deleted and makes the request to Laravel.

$scope.btnEliminar = function (_id) {
    $http({
        method: 'DELETE',
        url: 'http://urldelaapi.com/api/v1/eventos/' + _id
    }).success(function (data) {
        console.log(data);
        alert(data);
    }).error(function (data) {
        console.log(data);
        alert('Hubo un error');
    });
}

You can base yourself on this example: link

    
answered by 08.10.2018 / 23:32
source