Awar Pulldozer
Awar Pulldozer

Reputation: 1101

use variables from out side function laravel 5.3

i have resource controller and inside the resource controller the index function

public function index()
{
    //
}

now i want to use variable from out side the function like this

public $data = "data";
public function index() use ($data)
{
    return $data;
}

i gat this error

syntax error, unexpected 'use' (T_USE), expecting ';' or '{'

i tried it as function like this

public function data()
{
    $data = "data";
    return $data;
}
public function index() use (data)
{

}

i gat the same error

syntax error, unexpected 'use' (T_USE), expecting ';' or '{'

so how can i access variable and function from outside the function ..

Upvotes: 1

Views: 39

Answers (1)

Troyer
Troyer

Reputation: 7013

You need to use this to access to the variable:

public $data = "data";

public function index()
{
    return $this->data;
}

Upvotes: 3

Related Questions