cloudcake / laravel-approval

Attach an approval process to any model, approve and disapprove from any other model, for Laravel.
MIT License
143 stars 15 forks source link

Argument 1 passed to App\User::approve() must be an instance of Approval\Models\Modification #13

Closed afarral closed 4 years ago

afarral commented 4 years ago

I have out a button against each modification record so that the user can approve or reject, i achive this by having a route defined: Route::get('absence/verificationApprove/{modification_id}','AbsenceController@verificationApprove')->name('absence.approve'); Route::get('absence/verificationReject/{modification_id}','AbsenceController@verificationReject')->name('absence.reject');

then in the controller function i have:

    public function verificationApprove($modification_id)
    {

        $modification = Modification::with('modifiable')
            ->where('id',$modification_id)
            ->get();

        $approver = Auth::user();
        $approver->approve($modification);

        $changes=Modification::with('modifiable')->get();
        return view('hrmodule::absences.verifications', compact('employee_id','changes')); 

    }

but i get the error: Argument 1 passed to App\User::approve() must be an instance of Approval\Models\Modification, instance of Illuminate\Database\Eloquent\Collection given, called in D:\LARAVEL_GRCARE\GRCARE\Modules\HRModule\Http\Controllers\AbsenceController.php on line 102

but as far i can see the Modifcation:: should mean that the correct type is returned, but it is insisting that i an supplying a collection. I use the where as i want a single modificaton record to approved/rejected rather than all.

Where/what am i not doing correctly? Thanks Asda

stephenlake commented 4 years ago

You cannot approve a collection of models, only a single model.

$modification = Modification::with('modifiable') ->where('id',$modification_id) ->get();

You are returning a collection here. Use first or find instead of get - this is covered in the Laravel documentation. Please search for answers to your questions online as they have nothing to do with this package.

See the Laravel Eloquent Collections documentation. :page_facing_up: RTFM.