【发布时间】:2014-11-04 15:25:51
【问题描述】:
所以我是 Laravel 的新手。 我知道如何将数组传递给视图。我也试图通过将其他变量传递给视图来改变这个基本概念。
这是我的路线:
Route::get('sites', function()
{
$thosting = DB::table('sites')
->select(DB::raw('sum(hosting_fee) as thosting'))
->where('active', True)
->get();
$tdomain = DB::table('sites')
->select(DB::raw('sum(domain_fee) as tdomain'))
->where('active', True)
->get();
$atotal = $thosting + $tdomain;
$sites = Site::where('active', True)->orderBy('updated_at', 'DESC')->get();
return View::make('sites')
->with('sites', $sites)
->with('thosting', $thosting)
->with('tdomain', $tdomain)
->with('atotal', $atotal);
//var_dump($sites);
});
这里是查看sites.blade.php
@extends('layouts.master')
@section('content')
<div class="container well">
<table class="table-condensed">
<th>Annual Hosting</th><th>Annual Domain Name</th><th>Total Annual Income</th>
<tr>
<td>{{ thosting }}</td>
<td>{{ tdomain }}</td>
<td>{{ atotal }}</td>
</tr>
</table>
<h1>Hosting Accounts - Nearest to expiration </h1>
@foreach($sites as $site)
<a href="{{ URL::to('edit', $site->id) }}"><p>{{ $site->host_renewal_date }} - {{$site->site}}</p></a>
@endforeach
@stop
</div>
$site->Id 等按预期显示站点列表。但是当我尝试像 {{ thosting } 一样显示它们时,我传递的 3 个新变量 thosting、tdomain 和 atotal 会导致错误。错误是ErrorException,已定义(E_unknown)
Use of undefined constant thosting - assumed 'thosting' (View: C:\Users\myname\Desktop\websites\laravel\first\app\views\sites.blade.php)
我假设我没有正确访问我尝试过的变量 回声主机
那也没用。文档没有解决返回多个变量或数组变量的问题。感谢经验丰富的兽医提供的任何帮助。最后,我想弄清楚的是如何从几个数据库查询中返回视图数据。
谢谢
【问题讨论】:
-
您不必为每个变量调用
with。您可以将关联数组作为第二个参数传递给View::make。 -
Scheisse_minelli 我的主要问题是不知道如何在刀片中显示变量。如果关联数组有帮助,如果您能告诉我如何做,我很感兴趣。谢谢
-
第一个答案是完全准确的。在
{{ $var }}中,您只需将其视为一个小的 php 语句。它大致相当于<?php echo $var; ?>。在with方法中,第一个参数(字符串)将是变量到达视图后的名称。如果第一个答案对您不起作用,那么这些变量很可能在到达时未定义。 -
我刚试过 。它给出了数组到字符串的转换错误。我在“with”方法中的第一个参数称为托管。当我 print_r 或 var_dump 时,路径和视图中的 $thosting 都显示为非空。
标签: laravel laravel-4 routing views