2016-12-10 57 views
0

我正在爲我的dovecot數據庫創建基於Web的界面。即使填充了Laravel視圖也不會循環模型

我可以很容易地獲得數據庫中所有虛擬域的列表以及電子郵件和別名的數量。

但是,當我嘗試加載頁面列出特定域下的電子郵件時,它變得很奇怪。

三個簡單的模型:

<?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 
{ 
    // 
} 

我的默認控制器:

<?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), 
      ]); 
    } 
} 

和幾個簡單的葉片

home.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> 

和域.blade.php

<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> 

視圖輸出域下的正確數量的電子郵件與{{ $emails->count() }} - but the @foreach($電子郵件爲$電子郵件)`不循環。

當我修改刀片簡單的使用從域變量({{ $domain->emails->count() }}和​​)的電子郵件,我得到正確的計數和列表無誤。

使用電子郵件變量時出現什麼問題?

回答

1

你必須做一個小的變化,它的工作

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

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

沒有->get()你會得到一個查詢生成器實例,同時與get()將返回一個集合。在foreach循環中,可以在查詢構建器實例無法迭代的情況下迭代集合。

希望這會有所幫助

相關問題