2016-12-10 8 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

あなたはそれがコレクションを返しますget()としているときに、クエリビルダのインスタンスを取得します->get()なければ

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

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

を動作させるために小さな変更を行う必要があります。 foreachループでは、クエリービルダーインスタンスができない間にコレクションを反復することができます。

希望すると便利です。

関連する問題