即使填充了Laravel视图也不会获得循环模型

时间:2016-12-10 04:54:57

标签: laravel-5 eloquent laravel-blade

我为我的dovecot数据库创建了一个基于Web的界面。

我可以很容易地获得数据库中所有虚拟域的列表以及电子邮件和别名的数量。

但是当我尝试加载页面以列出特定域下的电子邮件时,这很奇怪。

三个简单的模型:

interface EventTarget { result: any; }

我的默认控制器:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class VirtualDomain extends Model
{
    public function emails()
    {
        return $this->hasMany('App\VirtualUser', 'domain_id');
    }

    public function aliases()
    {
        return $this->hasMany('App\VirtualAlias', 'domain_id');
    }
}

class VirtualUser extends Model
{
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
    ];
}

class VirtualAlias extends Model
{
    //
}

和几个简单的刀片

home.blade.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\VirtualDomain;
use App\VirtualUser;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('home', [
                'domains' => VirtualDomain::all(),
            ]);
    }



    public function domain($name)
    {
        $domain = VirtualDomain::where('name', $name)->first();

        return view('domain', [
                'domain' => $domain,
                'emails' => VirtualUser::where('domain_id', $domain->id),
            ]);
    }
}

和domain.blade.php

<p>
    {{ $domains->count() }} domains
</p>

<table class="table-summary">
    <thead>
        <tr>
            <th>name</th>
            <th>emails</th>
            <th>aliases</th>
            <th class="table-summary-options">options</th>
        </tr>
    </thead>

    <tbody>
        @foreach ($domains as $domain)
            <tr>
                <td><a title="view emails for this domain" href="domain/{{ $domain->name }}">{{ $domain->name }}</a></td>
                <td>{{ $domain->emails->count() }}</td>
                <td>{{ $domain->aliases->count() }}</td>
                <td class="table-summary-options"><a class="ui-action" title="remove this domain" href=""><img src="/img/ui/remove.png" alt="remove"></a></td>
            </tr>
        @endforeach
    </tbody>
</table>

该视图使用<p> <a href="/">&lt;&lt;</a> - {{ $domain->name }} - {{ $emails->count() }} emails </p> <table class="table-summary"> <thead> <tr> <th>email</th> <th class="table-summary-options">options</th> </tr> </thead> <tbody> @foreach ($emails as $email) <tr> <td><a title="view aliases for this domain" href="email/{{ $email->email }}">{{ $email->email }}</a></td> <td class="table-summary-options"><a class="ui-action" title="remove this email" href=""><img src="/img/ui/remove.png" alt="remove"></a></td> </tr> @endforeach </tbody> </table> @ foreach($ emails as $ email)在域下输出正确数量的电子邮件``不循环。

当我修改刀片以简单地使用来自域变量({{ $emails->count() }} - but the{{ $domain->emails->count() }})的电子邮件时,我得到了正确的计数,并且列表已正确填充。

使用电子邮件变量时出现了什么问题?

1 个答案:

答案 0 :(得分:1)

你必须做一个小改动才能工作

var dataStackLayout = d3.layout.stack()(dataIntermediate);

x.domain(dataStackLayout[0].map(function (d) {
    return d.x;
}));

y.domain([0,
    d3.max(dataStackLayout[dataStackLayout.length - 1],
            function (d) { return d.y0 + d.y;})
    ])
  .nice();

如果没有 public function domain($name) { $domain = VirtualDomain::where('name', $name)->first(); return view('domain', [ 'domain' => $domain, 'emails' => VirtualUser::where('domain_id', $domain->id)->get(), ]); } ,您将获得一个查询构建器实例,而->get()将返回一个集合。在foreach循环中,可以迭代集合,而查询构建器实例则不能。

希望这有帮助