php – Why isn't my model deleted even if I passed it to the controller?-ThrowExceptions

Exception or error:

I have a button to delete an entry of a certain table. The controller is a nested controller. It takes the building and the buildingWork variable.

When I dump the variables in my view, I get a true model and everything. However, when I dump the data in my controller, it returns an empty object. I don’t get why it is doing that…

Here is my code :

Controller

    public function destroy(Building $building, BuildingWork $buildingWork)
    {
        dump($buildingWork);
        $buildingWork->delete();
        // return redirect()->route('buildings.apartments.index', $building);
    }

View

@foreach($building->buildingWorks->sortByDesc('urgent') as $buildingWork)
        <tr class="table-{{$buildingWork->getColoring()}}">
            <td>{{$buildingWork->description}}</td>
            <td align="right">
                <a type="button" class="btn btn-success" href="">Marquer comme "fait"</a>
                <form class="d-inline-block" method="post" action="{{route('buildings.buildingworks.destroy', [$building, $buildingWork])}}">
                    @method('DELETE')
                    @csrf
                    <input type="submit" class="btn btn-danger" value="Annuler" />
                </form>            
            </td>
        </tr>
        @endforeach

So when I click on the button, the model is not deleted. According to Telescope, there isn’t even a request that tries to delete it…

DUMPS

In view:

App\BuildingWork {#498 ▼
  #guarded: []
  #connection: "sqlite"
  #table: "building_works"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:6 [▶]
  #original: array:6 [▶]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
}

In controller :

App\BuildingWork {#419 ▼
  #guarded: []
  #connection: null
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #attributes: []
  #original: []
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
}

Routes :

Route::resource('buildings', 'BuildingController');
Route::resource('buildings.apartments', 'ApartmentController');
Route::resource('buildings.apartments.payments', 'PaymentController');
Route::resource('buildings.buildingworks', 'BuildingWorkController');
How to solve:

you can test this

    public function destroy(BuildingWork $buildingWork)
{
    $buildingWork->delete();
    // return redirect()->route('buildings.apartments.index', $building);
}

and view

@foreach($building->buildingWorks->sortByDesc('urgent') as $buildingWork)
    <tr class="table-{{$buildingWork->getColoring()}}">
        <td>{{$buildingWork->description}}</td>
        <td align="right">
            <a type="button" class="btn btn-success" href="">Marquer comme "fait"</a>
            <form class="d-inline-block" method="post" action="{{route('buildings.buildingworks.destroy', [$buildingWork])}}">
                @method('DELETE')
                @csrf
                <input type="submit" class="btn btn-danger" value="Annuler" />
            </form>            
        </td>
    </tr>
    @endforeach

Answer:

I was able to solve my issue by tweaking the controller a little bit.

I noticed that if I removed the “BuildingWork” before $buildingWork, I had an id. So, I used this id to find the proper model and delete it.

public function destroy(Building $building, $buildingWork)
    {
        $i = BuildingWork::find($buildingWork);
        $i->delete();
        return redirect()->route('buildings.apartments.index', $building);
    }

Leave a Reply

Your email address will not be published. Required fields are marked *