Delete in laravel using id

0

I have a very simple method, that the only thing that it does is take the id from a form, to erase the insertion, the issue is that it does not work for me:

Vista: I have a form, in which you select the id of the vacation and I pass it to the method

<form action="{{url('vacation/delete')}}" method="POST" role="form"> 
    {{csrf_field()}}

    <legend>Solicitudes Pendientes</legend>
    <select name= "vacation_id" id="inputVacation_id" class = "form-control" required="required">
        @foreach ($vacations as $vacation)
            <option >{{$vacation->name}}</option>
        @endforeach
    </select>

    <button type="submit" name="delete" class= "btn btn-primary"><i class="icon-cross2"></i></i></button>
</form>

Then in the controller:

public function delete(Request $request){

    //$vac = Vacation::find($request['vacation_id']);
    //$res=Vacation::where('id',$vac)->delete();
    Vacation::destroy($request['vacation_id']);

    return redirect()->back();
}

public function delSolicitudes()
{
    $vacations =  \DB::table('vacations')
        ->select('workers.name','vacations.id' ,'vacations.type','vacations.date_from','vacations.date_to','vacations.observations','vacations.aceptado')
        ->join('workers','workers.id','=','vacations.worker_id')
        ->where(['vacations.aceptado' => '0'])
        ->get();

    return view('vacation.deletevac', ['vacations' => $vacations]);              
}

You can see that I simply have the query to get the data, that I really do not need any more than the id, but when I give it to delete it does not erase anything, it does not give an error, but it does not do anything either,

Question 2: Comment that I have tried to use the same form, which I use to update with the delete, and it has not worked for me, would it be possible to update and delete in the same form? whereas you have to falsify the methods?

Greetings and thanks.

    
asked by Peisou 23.02.2018 в 09:53
source

2 answers

2

Good I think the error is that in < option > you do not spend any value

<legend>Solicitudes Pendientes</legend>
   <select name= "vacation_id" id="inputVacation_id" class = "form-control" required="required">
       @foreach ($vacations as $vacation)
          <option name= "vacation" value="{{$vacation->id}}">{{$vacation->name}}</option>
       @endforeach
    </select>

Answering your second question, if it can be done, using two buttons, one modify and one delete. In the controller you use a condition if you press the button modify action update if you press delete button delete action.

I would not do it that way, I would do it as Laravel recommends with two routes, and two functions. It is cleaner and simpler in the future.

    
answered by 23.02.2018 / 11:02
source
1

When you are passing or collecting values from a view to manipulate them in the controller, you should have the following:

<form action="{{url('vacation/delete')}}" method="POST" role="form"> 
    {{csrf_field()}}

    <legend>Solicitudes Pendientes</legend>
    <select name= "vacation_value" id="inputVacation_id" class = "form-control" required="required">
        @foreach ($vacations as $vacation)
            <option value="{{ $vacation->id }}">
                {{$vacation->name}}
            </option>
        @endforeach
    </select>

    <button type="submit" name="delete" class= "btn btn-primary"><i class="icon-cross2"></i></i></button>
</form>

In the previous way, when you pass the data the value of the id is dynamic and existing, since it is being generated by each query and as you can also notice the value is the value that will help me to work in the controller.

Already in the controller you can do the following:

$tabla = Tabla::find($id);
$tabla->delete();

The above as an example, check the name that you put to the select and the name is the same in the get method in the controller

    
answered by 23.02.2018 в 22:43