Thursday, March 30, 2017

Delete item from a pivot table

I try to delete an item form a pivot query , and i'm not sure if i do the right method .

Here my view with some "football stades" for a structure (club)

structure can have many stades and stades have many structures

here my model Stade :

public function structures()
    {
        return $this->belongsToMany('App\Structure');
    }

here my model Structure :

 public function stades(){

        return $this->belongsToMany('App\Stade');

    }

My pivot table is like:

structure_id 
stade_id




@foreach($club->stades as $stade)
                        <tr>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th>
                                
                                
                                


                            </th>
                        </tr>
                        @endforeach

I can insert a new stade to a structure without any problems

 public function store(Request $request , $id){

    $stade_structure = new ClubStade;

    $structure =  Structure::findOrFail($id);

    $stade_structure->structure_id = $structure->id;

    $stade_structure->stade_id = $request->input('stade_id');

    $stade_structure->save();

        return back()->with('status', "Un nouveau stade à été ajouté pour votre Club ! ");
    }

but now i would like to detach or delete the record from the table . do i need to make a delete function ?

thanks a lot in advance



via Mathieu Mourareau

Advertisement