rcrowe/TwigBridge

Call function from Model within Twig View returns null

creazy231 opened this issue · 4 comments

Setup:

// User Model
class User extends Model
{
    ...
    public function someFunctionName() {
        return "TEST";
    }
}
// UserController
class UserController extends Controller
{
    public function index($sub = null) {
        $workers = User::all();
        return View::make('users/users', ['sub' => $sub, 'workers' => $workers]);
    }
}
// frontend.twig
{% for user in workers %}
        {{ dump(user.someFunctionName) }}
    {% endfor %}

The twig view (dump) returns null instead of TEST.
Whats wrong with my setup?

And with parentheses?

Where should I put them?

nVuln commented

user.someFunctionName => get property someFunctionName (your case)
user.someFunctionName() => call method someFunctionName (try this)

Problem solved!
In my case I had two different User Models. One created by Laravel and one created by calling php artisan code:models and I didn't notice that Laravel was using the default User Model.

user.someFunctionName() works like expected. Thanks @ndhan93