Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
bhushan08 opened this issue Sep 2, 2021 · 0 comments

Comments

@bhushan08
Copy link

bhushan08 commented Sep 2, 2021

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'); });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant