1

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

2
  • 1
    if(!isset($user)) this execute when token is null... if isset token then what you want? where is else condition? Commented May 10, 2017 at 7:24
  • You won't get anything If your if(!isset($user)) condition fails. So write some else to your if. Commented May 10, 2017 at 12:37

3 Answers 3

1

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

    }
}   
Sign up to request clarification or add additional context in comments.

Comments

1

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

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

return $flight;

Comments

0

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

1 Comment

Now can a call the method from public function submit: if(!$check_token['success']) return $check_token;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.