Call function from Model within Twig View returns null
creazy231 opened this issue · 4 comments
creazy231 commented
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?
barryvdh commented
And with parentheses?
creazy231 commented
Where should I put them?
nVuln commented
user.someFunctionName => get property someFunctionName (your case)
user.someFunctionName() => call method someFunctionName (try this)
creazy231 commented
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