cloudcreativity / laravel-json-api

JSON API (jsonapi.org) package for Laravel applications.
http://laravel-json-api.readthedocs.io/en/latest/
Apache License 2.0
778 stars 109 forks source link

Arguement 3 passed to controller must be an instance of model, string given #623

Closed bhushan08 closed 3 years ago

bhushan08 commented 3 years ago

Hello, first of all, great library, very useful, thank you for creating this wonderful library.

I have created a custom controller which consists of one action. I want to update the status of the shop employee on this action. When I post on the action url, that is, shop_employees/2/-actions/activate, I get this error Argument 3 passed to App\\Http\\Controllers\\API\\V1\\ShopEmployeeEditController::activate() must be an instance of App\\Models\\ShopEmployee, string given, called in /home/bhushan/Documents/Projects/Repo/where-hair-backend/vendor/laravel/framework/src/Illuminate/Routing/Controller.php on line 54",

I checked in the string for the third parameter I am getting "shop_employees". I should get the model, where am I going wrong?

Source code as follows, as per the documentation:

ShopEmployeeEditController.php

`<?php

namespace App\Http\Controllers\API\V1;

use App\Http\Controllers\Controller; use App\JsonApi\V1\ShopEmployees\ShopEmployeeSchema; use App\Models\ShopEmployee; use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Auth\AuthenticationException; use LaravelJsonApi\Core\Responses\DataResponse; use LaravelJsonApi\Laravel\Http\Controllers\Actions;

class ShopEmployeeEditController extends Controller {

use Actions\FetchMany;
use Actions\FetchOne;
use Actions\Store;
use Actions\Update;
use Actions\Destroy;
use Actions\FetchRelated;
use Actions\FetchRelationship;
use Actions\UpdateRelationship;
use Actions\AttachRelationship;
use Actions\DetachRelationship;

/**
 * @param ShopEmployeeSchema $shopEmployeeSchema
 * @param ShopEmployee $shopEmployee
 * @return mixed
 * @throws AuthorizationException
 * @throws AuthenticationException
 */
public function activate(ShopEmployeeSchema $shopEmployeeSchema, $query, ShopEmployee $shopEmployee) {
    if (!empty(auth()->user())) {
        $shopEmployee->update([
            "status" => 'active',
        ]);
    }

    return $this->reply($shopEmployeeSchema, $query, $shopEmployee);
}

private function reply($schema, $query, $post) {
    $model = $schema
        ->repository()
        ->queryOne($post)
        ->withRequest($query)
        ->first();

    return new DataResponse($model);
}

}`

api.php

$server->resource('shop_employees', \App\Http\Controllers\API\V1\ShopEmployeeEditController::class)->actions('-actions', function ($actions) { $actions->withId()->post('activate'); });