Edwin Delgado
Edwin Delgado

Reputation: 21

How to return an array after calling a function in a laravel controller

I´m using laravel. My UserController call a public function (validateToken) which is placed on the top of the controller. I call my function from an another public function, but this doesn´t return anything. this is my code:

use App\Comment;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class UserController extends Controller
{
    public function validateToken($token)
    {
        $user = User::where('token', $token)->first();

        if(!isset($user))
        {
            return array(
                'message' => "These credentials do not match our records.",
                'success' => false
                };
        }
    }

    public function register(Request $request)
    {
        $input = Input::all();

        $this->validateToken($input['token']);

        return array(
            'message' => 'Your account has been created successfully and is ready to use', 
            'token'   => $input['token'], 
            'success' => true
           );
    }

It works if the validateToken code is placing inside the register function

Upvotes: 1

Views: 7935

Answers (3)

Abdulkadir Ugas
Abdulkadir Ugas

Reputation: 501

If you want to return array data use get() instead of first()

$flight = Flights::where([
            ['destination', '=', $destination],
        ])->get()->toArray();

return $flight;

Upvotes: 1

hoque
hoque

Reputation: 6471

You should process the return value after calling that function.

use App\Comment;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class UserController extends Controller
{
    public function validateToken($token)
    {
        $user = User::where('token', $token)->first();

        if(!isset($user))
        {
            return array(
                'message' => "These credentials do not match our records.",
                'success' => false
                };
        }else {
            return array(
                'message' => 'Your account has been created successfully and is ready to use', 
                'token'   => $token, 
                'success' => true
               );
        }


    }

    public function register(Request $request)
    {
        $input = Input::all();

        return $this->validateToken($input['token']);

    }
}   

Upvotes: 1

Patryk Woziński
Patryk Woziński

Reputation: 746

Which method are you calling from other classes?

validateToken() should be in separated class anyway. Place it to the User model like:

// \App\User model
public static function isValidToken($token)
{
    $user = self::where('token', '=', (string)$token)->first();

    return [
        'success' => $user !== null,
        'message' => $user === null
            ? 'These credentials do not match our records.'
            : 'User found, token is valid',
    ];
}

Now u can get access to this method from anywhere in your Laravel application by:

$check_token = User::isValidToken('my-token');

Upvotes: 0

Related Questions