1

モデル、データベース、コントローラーが別々のフォルダーにある Laravel 5.3 でアプリケーションを構築しようとしています。次のフォルダー構造があります。

Nitseditor
    System
        Controllers
        Database
            2016_12_28_130149_create_domains_table.php
            2017_01_06_193355_create_themes_table.php
            2017_01_07_140804_create_themes_domains_table.php
        Models
            Domain.php
            Theme.php

私は多対多の関係を持つドメインで関係を築いています。

public function themes()
{
    return $this->belongsToMany('Nitseditor\System\Models\Domain');
}

domain_theme内のテーブルに名前を付けました2017_01_07_140804_create_themes_domains_table.php

今、コントローラーのドメインに属するテーマ名を次のように取得しようとしています:

$flashmesage = new Domain;

foreach ($flashmesage->themes as $theme)
{
    return $theme->theme_name;
}

エラーが発生します:

SQLSTATE [42S02]: ベース テーブルまたはビューが見つかりません: 1146 テーブル 'nitswebbuilder.domain_domain' が存在しません (SQL: select domains.*, domain_domain. domain_idas pivot_domain_idfrom domainsinner join domain_domainon domains. id= domain_domain. domain_idwhere domain_domain. domain_idis null and domains. deleted_atis null)

4

2 に答える 2

0

テーブル名は と呼ぶ必要がありますdomain_theme。外部キーに適切な名前を使用し、正しい関係を構築している場合は、次のwhereHas()ように機能します。

$domainName = 'example.com';

$themes = Theme::whereHas('domains', function($q) ($domainName) {
    $q->where('domain_name', $domainName);
})->get();

次に、すべてのテーマ名を表示します。

@foreach ($themes as $theme)
    {{ $theme->theme_name }}
@endforeach
于 2017-01-07T15:18:41.767 に答える