mardi 28 novembre 2017

CRUD -> Destroy Method

I'm not sure how to make a destroy method yet. Can you guys give me a hand here?

I'm using this form:

@foreach($data as $d)
<tr>
<td> </td>
<form method="POST" action="/newuser/delete/{$d->id}">
<input type="hidden" _method="DELETE">   
<td><button type="submit" class="btn btn-default btn-sm">Excluir</button></i></a></td>
</form>
</tr>

@endforeach 

This Controller:

    public function search(Request $request)
    {
    $users = Users::search($request);
    $users1 = Groups::search($request);
    foreach ($users as $user) {
    $data[] = array('Username' => $user->username,
    'id' => $user->id
    );
    };
    foreach ($users1 as $user1){
    $data1[] = array ('id2' => $user1->id);
    };
    return view('user.buscar', compact('data'));
    }

    public function buscar()
    {
    return view ('user.buscar');
    }

    public function destroy($id)
    {
    try {
    DB::transaction(function() use($id)
    {
    $user = Users::find($id);
    $user1 = Groups::find($id);
    $user->delete();
    $user1->delete();
    });
    return redirect('home')
    ->with('message', 'Usuário removido com sucesso!!');
    }
    catch (\Exception $e) {
    return redirect('home')
    ->with('message', 'Usuário não foi removido, verifique os dados!!');
    }

And using this route Route::delete('newuser/delete/{$d->id}', 'UserController@destroy');

I not sure if what I'm doing is right. Please, Be patient I'm new to this.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire