1

I'm new with Laravel and I'm having problems while I try to detach some values from my pivot tables.

The pivot tables I'm woking with are: excercise_workout (ejercicio_rutina) & excise_day (ejercicio_dia) (those tables have just two id's each one). The relations are ManyToMany. A workout have a lot of days and a day have a lot of excercises.

I've created these routes:

Route::resource('rutinas','RutinasController'); Route::delete('rutinas.destroyEjercicio/{id}','RutinasController@destroyEjercicio');

I have this button that is a form and call the delete:

                {!! Form::open([
                    'method' => 'DELETE',
                    'action' => [ 'RutinasController@destroyEjercicio', $ejercicio->id ]
                ]) !!}
                    {!! Form::submit('Borrar Ejercicio', ['class' => 'btn btn-danger']) !!}
                {!! Form::close() !!}

The destroyEjercicio is the part I don't know how to hadle with:

public function destroyEjercicio($id)
{
    $ejercicio = Ejercicio::findOrFail($id);


    dias()->$ejercicio->detach($id);

   //EXAMPLE DELETE WITH QUERY 
   // DB::delete('delete from toy_user where toy_id = ? AND user_id = ? AND status = 0', array($toy->id,Auth::id()));

    return redirect('rutinas');

}

I recieve this error when I try to submit the delete form: Call to undefined function App\Http\Controllers\dias()

I've tried a lot of other methods but I don't know how to do it.

I need to know the ID of the day and the ID of the excercise I want to delete. And I don't know how the past it by the post request.

UPDATE -> THIS IS THE FORM I DO NOT HAVE INPUTS TO REQUEST...

@foreach ($rutina->dias as $dia)
    <div class="col-md-6 col-md-offset-3">
        <div class="panel panel-default">
          <div class="panel-body">
            Ejercicios para el <strong>{{ $dia->nombre }}</strong>
            <hr>
            @foreach ($dia->ejercicios as $ejercicio)
                <ul>
                    <li>{{ $ejercicio->nombre }}</li>
                </ul>
                {!! Form::open([
                    'method' => 'DELETE',
                    'action' => [ 'RutinasController@destroyEjercicio', $ejercicio->id ]
                ]) !!}
                    {!! Form::submit('Borrar Ejercicio', ['class' => 'btn btn-danger']) !!}
                {!! Form::close() !!}
             @endforeach
          </div>
        </div>
    </div>
@endforeach 
  • `foreach ($rutina->dias as $dia)
    foreach ($dia->ejercicios as $ejercicio)
    • {{ $ejercicio->nombre }}
    {!! Form::open([ 'method' => 'DELETE', 'action' => [ 'RutinasController@destroyEjercicio', $ejercicio->id ] ]) !!} {!! Form::submit('Borrar Ejercicio', ['class' => 'btn btn-danger']) !!} {!! Form::close() !!} endforeach endforeach`
    – portobello francisco May 09 '16 at 17:28

2 Answers2

0

You need something like this:

public function destroyEjercicio($ejercicioId, $diasId)
{
    $ejercicio = Ejercicio::findOrFail($ejercicioId);

    $ejercicio->dias()->detach($diasId);

https://laravel.com/docs/5.1/eloquent-relationships#inserting-many-to-many-relationships

Update

How to use Request object. If you're using forms, Reuqest object will contain all form fields. Just do something like this:

public function destroyEjercicio(Request $request)
{
    $ejercicioId = $request->get('ejercicioIdField');

    $diasId = $request->get('diasIdField');
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • I understand you and it have sense, but where I have to specify this $ejercicioId, $diasId ? On the form? – portobello francisco May 09 '16 at 17:22
  • Usually you have a form which you fill and send it to `destroy()` method. So just make sure user fills day and exercise and pass the data. If you're using forms you can get data from Request object. Please check updated answer for example code. – Alexey Mezenin May 09 '16 at 17:27
  • I update so you can see my form i do not have inputs to request it's just a button inserted in every excercise – portobello francisco May 09 '16 at 17:33
0

I solved this the following way. First I create this new route:

Route::post('rutinas.destroyEjercicio/{ejercicioId}/{diaId}', [
'as' => 'destroy', 
'uses' => 'RutinasController@destroyEjercicio'
]);

You must add this array at the form:

                {!! Form::open([
                    'method' => 'POST',
                  *******'route' => ['destroy', $ejercicio->id, $dia->id]******
                ]) !!}
                    {!! Form::submit('Borrar Ejercicio', ['class' => 'btn btn-danger']) !!}
                {!! Form::close() !!}

Then in your controller you pass the two id's that you need:

public function destroyEjercicio($ejercicioId, $diaId)
{
    $ejercicio = Ejercicio::findOrFail($ejercicioId);
    $ejercicio->dias()->detach($diaId);
    return redirect('rutinas');
}