我正在用Laravel开发一个列出脚本的网站。在我的视图文件中,我需要循环遍历脚本,单独列出符合特定条件的脚本的子代。我想要包含控制器中的所有功能,但是我无法看到如何将特定于循环脚本的信息传递到视图中。见下面的例子:
//CONTROLLER
class ScriptController extends BaseController {
public function instance()
{
$scripts = Script::all();
return View::make('script')->with(array(
'scripts' => $scripts
));
}
// VIEW
@foreach ($scripts as $script)
TITLE: {{ $script->title }}<br />
VOTES:
@foreach ($script->votes as $v)
{{ $v['vote'] }},
// This just lists out each vote (which is 1 or -1)
// how do I count all 1's then all the -1's separately without a where query?
@endforeach
@endforeach
最终,我需要在视图中执行类似下面的操作,或以某种方式通过控制器传递信息。
// IMPOSSIBLE VIEW
@foreach ($scripts as $script)
TITLE: {{ $script->title }}<br />
VOTES:
@foreach ($script->votes as $v)
GOOD VOTES: {{ count($v where $v['vote'] == 1) }},
BAD VOTES: {{ count($v where $v['vote'] == -1) }}
@endforeach
@endforeach
我缺少任何想法或基本控制器概念?
更新 希望这能解决我的问题。昨晚我创建了一个简单的控制器,它可以完美地提供信息,包括我需要显示单个脚本的单个脚本的所有子类。并且可以完美地创建该视图。有没有办法循环该视图。
请注意,我必须在控制器中实际包含脚本ID($ sid)才能使其正常工作。
//Controller Function
public function instance()
{
$sid = 27; //HAD TO PHYSICALLY INCLUDE SCRIPT ID
$script = Script::find($sid);
$user = $script->user()->first();
$uid = $user['id'];
$votes = array();
$username = $user['username'];
$good = Vote::good()->where('script_id', "=", "$sid")->count();
$bad = Vote::bad()->where('script_id', "=", "$sid")->count();
$voteQuery = Vote::where('script_id', "=", "$sid")->get();
foreach($voteQuery as $v) {
$votes[$v['user_id']] = $v['vote'];
}
return View::make('script')->with(array(
'script' => $script, //array
'votes' => $votes, //array
'user' => $user, //array
'username' => $username,
'good' => $good,
'bad' => $bad
));
}
然后在我看来显示一个脚本:
//VIEW
TITLE: {{$script->title}}<br />
UP VOTES: {{ count($good)}}<br />
DOWN VOTES: {{ count($bad)}}<br />
USERNAME: {{$username}}<br />
@foreach($votes as $k => $v)
{{ $k }} - {{ $v }}<br />
@endforeach
有没有办法循环显示所有脚本?通过循环$ script_id数组并将其传递给每个循环中的控制器?
答案 0 :(得分:0)
我建议您先将控制器中的$scripts
数组预处理,然后再将其传递给视图。
//CONTROLLER
class ScriptController extends BaseController {
public function instance()
{
$scripts = Script::all();
// do good and bad vote summations
foreach ($scripts->votes as $idx => $v ) {
// initialize the counters
if ( $idx == 0 ) {
$v['Good'] = 0;
$v['Bad'] = 0;
}
if ( $v['vote'] == 1 ) {
$v['Good']++;
}
if ( $v['vote'] == -1 ) {
$v['Bad']++;
}
}
return View::make('script')->with(array('scripts' => $scripts));
}
}
然后,您可以在视图中显示[&#39; Good&#39;]和[&#39; Bad&#39;]计数器。
答案 1 :(得分:0)
呼。 24小时开关,这里是:
通常答案很简单,让我感到愚蠢。我可以遍历控制器中的所有脚本,然后通过&#34; echoing&#34;视图而不是&#34;返回&#34;控制器中的视图多次呈现视图。
//CONTROLLER
public function instance()
{
$allScripts = Script::orderBy('created_at', 'DESC')->paginate(10);
foreach($allScripts as $s) {
$sid = $s['id'];
$script = Script::find($sid);
$user = $script->user()->first();
$uid = $user['id'];
$votes = array();
$username = $user['username'];
$good = Vote::good()->where('script_id', "=", "$sid")->count();
$bad = Vote::bad()->where('script_id', "=", "$sid")->count();
$voteQuery = Vote::where('script_id', "=", "$sid")->get();
foreach($voteQuery as $v) {
$votes[$v['user_id']] = $v['vote'];
}
echo View::make('script')->with(array(
'script' => $script, //array
'votes' => $votes, //array
'user' => $user, //array
'username' => $username,
'good' => $good,
'bad' => $bad
));
}
}
然后视图可以是相同的
//VIEW
TITLE: {{$script->title}}<br />
UP VOTES: {{ $good }}<br />
DOWN VOTES: {{ $bad }}<br />
USERNAME: {{$username}}<br />
@foreach($votes as $k => $v)
USER ID: {{ $k }} voted - {{ $v }}<br />
@endforeach
<hr />