Return Objects in php laravel

Viewed 26

I'm trying to return an object from a function but I'm getting the error is there any way I can do that?

<?php

namespace App\GraphQL\Mutations;

final class Login
{
    /**
     * @param  null  $_
     * @param  array{}  $args
     */
    public function __invoke($_, array $args)
    {
        
        return{
            ok: false,
            Error: "password not correct"
            token: "jhfkjnfknkfdj"
        };
}
1 Answers

Use an array casted as an object:

return (object)[
    'ok' => false,
    'Error' => "password not correct",
    'token' => "jhfkjnfknkfdj",
];

For Laravel:

return response()->json([
    'ok' => false,
    'Error' => "password not correct",
    'token' => "jhfkjnfknkfdj",
]);
Related