php - why do the Models work in routes and in controllers but not in views? -
i pretty experienced programmer in laravel , have worked on projects in laravel 4.2. have started working laravel 5, , bugging me, have spent hours on , haven't been able solve this.
whenever want grab model database table , set variable in order use it, seems works correctly when doing in routes.php or controller file, doesn't work in view files.
for example simple piece of code works in routes , controllers not in view files:
$user = app\models\user::find(1); echo $user->username;
if have view file doesn't contain html, except piece of code above this: see image http://i.imgur.com/whrcxyl.png
however if have html in view file , use same code this:
<html> <head> <title>example</title> </head> <body> <? $user = app\models\user::find(1); ?> <p>this doesn't work <?= $user->username; ?></p> </body>
i error undefined variable: user
i have no idea, small not seeing, can't see it. have made folder models , put models in folder..the code correct , works everywhere except in views if used directly above. table filled users , not empty.
the problem has been solved. thank treeface, overlooked didn't have php short tags enabled.
it's correct practice separate logic presentation it's best set variable in controller , pass variable view, if want or need set variable in view, should use <?php ?>
tags set variable. can output variable blade syntax.
<body> <?php $foo = 'bar'; ?> {{ $foo }} </body>
would output:
<body> bar </body>
Comments
Post a Comment