使用Laravel Transformers上的中间件在null上调用成员函数createData()

时间:2019-03-21 11:00:26

标签: php laravel eloquent transformer

因此,我创建了一种用于通过两种方法(token() / native)_)进行身份验证的控制器。我使用分形变压器返回响应。令牌方法对我来说很好用,但是loginAndroid()返回

  

“在null上调用成员函数createData()”错误。

有帮助吗?谢谢。

class AuthController extends RestController
{
    protected $transformer = UserTransformers::Class;

    public function __construct()
    {
        $this->middleware('auth:api', ['except' => ['login', 'loginAndroid']]);
    }

    public function login(Request $request)
    {
        $credentials = $request->only(['username', 'password']);

        if (!$token = auth()->attempt($credentials)) {
            return response()->json(['error' => 'Unauthorized'], 401);
        }

        return $this->respondWithToken($token);
    }

    public function loginAndroid(Request $request)
    {
        $credentials = $request->only(['username', 'password']);

        if (Auth::attempt($credentials)) {
            //$user = Auth::user()->with(['employees']);
            $userdata = User::with(['employees', 'employees.role', 'employees.branch'])->find(Auth::id());
            //$success['token'] =  $user->createToken('MyApp')->accessToken; 
            //return response()->json($userdata, 200); 
            //return $userdata;
            $response = $this->generateItem($userdata);

            return $this->sendResponse($response, 201);
        } else {
            return response()->json('gagal', 401);
        }
    }
}

这是我的restcontroller

abstract class RestController extends Controller
{
protected $manager;

protected $transformer;

public function __construct()
{
    $this->manager = new Manager();
}

protected function generateItem($model, $transformer = null)
{
    if (!is_null($transformer)) {
        return new Item($model, new $transformer);
    }

    return new Item($model, new $this->transformer);
}

protected function generateCollection($model, $transformer = null)
{
    if (!is_null($transformer)) {
        return new Collection($model, new $transformer);
    }

    return new Collection($model, new $this->transformer);
}

protected function sendResponse(ResourceInterface $data, $status = 200)
{
    return response()->json(
        $this->manager->createData($data)->toArray(),
        $status
    );
}

protected function sendNotFoundResponse($status)
{
    return response()->json($status, 404);
}

protected function sendIseResponse($status)
{
    return response()->json($status, 500);
}
}

1 个答案:

答案 0 :(得分:1)

您的sendResponse()方法似乎取决于$this->manager。但是,$this->manager已在RestController::__construct()中设置,并且您已经覆盖了__construct()中的AuthController::__construct()方法。因此,为了使$this->manager可用,您应该从AuthController调用父构造函数,如下所示:

class AuthController extends RestController
{
    protected $transformer = UserTransformers::Class;

    public function __construct()
    {
        parent::__construct(); // call the parent constructor where
                               //  $this->manager gets initialized
        $this->middleware('auth:api', ['except' => ['login', 'loginAndroid']]);
    }

... etc