mardi 3 octobre 2017

How to disable a Global Scope when explicitly asking for one Model by primary key

I have a global scope called Abandoned on my Deal model that helps hide deals that don't have enough user input to be worth interacting with.

The problem is, a Deal is automatically in the Abandoned state when it's created, and the user still needs to be able to interact with it, including calling various API URLs that contain Deal, e.g.,

Route::post('deals/{deal}/notes', 'DealController@add_note');

I want to be able to use the magic route key lookup so that controller method can work like:

public function add_note(Request $request, Deal $deal)

But if I write the controller like this, you can't add a Note to any Deal that is Abandoned. (You get a 404)

The only workaround I've found is to write every controller like:

public function add_note(Request $request, $deal_id){
  $deal = Deal::withAbandoned()->find($deal_id);

It feels like an expectation mismatch -- the app wants to hide Abandoned Deals hidden from all lists and reports, but if you know a deal "by name" (primary key) then you can continue to act on it.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire