2016-07-26 5 views
0

私は自分のビュー内に、自分のロケーションテーブル(Addr、Cityなど)の情報とリソーステーブル(Name、Descr)の情報で構成される内容を表示しようとしています。 ResourceLocationテーブルここで私がこれまで持っているものです。ビュー内のテーブルを通してhasMany()関係を表示する方法。

リソースコントローラ

.... 
 

 
public function resource() 
 
{ 
 
    $resources = Resource::all(); 
 
    return view('pages.resource', compact('resources')); 
 
} 
 

 
public function location() 
 
{ 
 
    $locations = Location::all 
 
    return view (compact('locations')); 
 
} 
 

 
public function resourcelocation() 
 
{ 
 
    $resourcelocations = ResourceLocation::all(); 
 
    return view (compact ('resourcelocations')); 
 
} 
 

 
...
ResourceLocationモデル

/** 
 
* Class ResourceLocation 
 
*/ 
 
class ResourceLocation extends Model 
 
{ 
 
    ... 
 

 
    public function resource() 
 
    { 
 
     return $this->belongsTo('App\Models\Resource'); 
 
    } 
 

 
    public function location() 
 
    { 
 
     return $this->belongsTo('App\Models\Location'); 
 
    }  
 
}

リソース・モデル

/** 
 
* Class Resource 
 
*/ 
 
class Resource extends Model 
 
{ 
 

 
... 
 

 
/** A resource can have many locations */ 
 

 
    public function location() 
 
    { 
 
    return $this->hasMany('App\Models\ResourceLocation'); 
 
    }  
 
}
場所モデル

/** 
 
* Class Location 
 
*/ 
 
class Location extends Model 
 
{ 
 
... 
 
    public function resource() 
 
    { 
 
     return $this->hasMany('App\ResourceLocation'); 
 
    } 
 
}
resource.blade.php

そう、

<table> 
 
    <tr> 
 
     <th>Resource Name</th> 
 
     <th>Description</th> 
 
     <th>Address</th> 
 
     <th>City</th> 
 
     <th>Zip Code</th> 
 
     <th>County</th> 
 
    </tr> 
 
    @foreach ($resourcelocations as $resourcelocation) 
 
    <tr> 
 
     <td> {{ $resourcelocation->id }}</td> 
 
     <td> 
 
     @foreach ($resourcelocation->resources as $resource) 
 
      {{ $resource->Name }}, 
 
     @endforeach 
 
     </td> 
 
     <td> 
 
     @foreach($resourcelocations->locations as $location) 
 
      {{ $location->City }} 
 
        @endforeach 
 
     </td> 
 
     </tr> 
 
     @endforeach 
 
</table>
は、私はちょうどそれが働いていたかどうかを確認するために列または2を追加したいが、私はまだlaravel周り私の頭をラップしようとしている、 resourcelocationsに未定義の変数を取得しておくと関係がどのように働きますか多分私の論理は台無しです。どんな助けも素晴らしいだろう!

ありがとうございました。

答えて

1

あなたの関係に混乱があるようです。私はそれがあなたが持っている方法ではうまくいくと思うが、必要以上に混乱させている。

まず、ResourceLocationモデルを削除します。仲介モデルを必要とせずに直接resourceslocationsを関連付けることができます(ただし、まだそのテーブルが必要です)。これは、所属関係と呼ばれるものです。

あなたは(リソースが多くの場所を持つことができる場合など)多くのレコードを返すことができる関係を持っているとき、私はまた、あなたがこれらのlocationsではなくlocationに関するメソッドに名前を付ける必要がありますお勧めします。心の中ですべてのことで

、あなたのResourceモデルは、あなたのピボットテーブルを名前の何でもする必要があります私は'ResourceLocation'を入れて...

public function locations() 
{ 
    return $this->belongsToMany('App\Models\Location', 'ResourceLocation'); 
} 

を取得します。

あなたのLocationモデルと同じです。

public function resources() 
{ 
    return $this->belongsToMany('App\Models\Resource', 'ResourceLocation'); 
} 

は今、それが大幅に簡素化されなければならない、我々はわずか2関係法及び2つのモデルの4つの関係法と3つのモデルを置き換えます。

コントローラの場合、すべてのリソースの場所を取得するために熱心な負荷を使用する方がはるかに効率的です。

$resources = Resource::with('locations')->get(); 

そして、あなたのビューで...

@foreach ($resources as $resource) 
    @foreach ($resource->locations as $location) 
     <tr> 
      <td>{{ $resource->id }}</td> 
      <td>{{ $location->name }}</td> 
     </tr> 
    @endforeach 
@endforeach 
+0

ありがとうございました。私はあなたがそのステートメントを使用するだけで2つのテーブルに参加できるかどうか分からず、私はSQL結合ステートメントを考えていました。 **編集**気にしないで、名前空間は異なっていました。進歩、私はテーブルを参照して、ちょうどデータがありません。 – ph0bolus

+0

'belongsToMany'関数は、IDのための追加パラメータをとります。 Laravelの予想と異なる場合は、データを返さない可能性があります。 'あなたの' ResourceLocation'テーブルにあなたのIDの名前は何ですか? – user3158900

+0

申し訳ありませんが、私はそれを理解しました。 '{{$ resource-> name}}'の代わりに、それは私のデータベースの 'Name'だったので、すべてが空白になりました。私がそれらをすべて大文字にした後、データは正しく表示されました。再度、感謝します! – ph0bolus

0

お客様のリソース・コントローラーでは、と表示されているため、resourcelocation変数は渡されていません。のより良いアイデアを得るために

$resource->location()->first(); 

ビューにデータを送信する前にdd(variable_name)を使用することができます。あなたは以下試すことができますhasManyの最初の要素を取得するには、このラインreturn view('pages.resource', compact('resources'));

に、アレイ内の変数を宣言してみてくださいあなたのデータがどのように管理されているか

関連する問題