Ich versuche, eine Tabelle in meiner Ansicht mit Inhalten anzuzeigen, die aus Informationen aus meiner Standorttabelle (Addr, Stadt, etc ...) und Ressourcentabelle (Name, Descr.) Bestehen, sie sind durch IDs miteinander verbunden eine Ressourcenstandort-Tabelle. Hier ist, was ich bisher:So zeigen Sie eine hasMany() - Beziehung über eine Tabelle in einer Ansicht an.
Resource Controller
....
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'));
}
...
/**
* Class ResourceLocation
*/
class ResourceLocation extends Model
{
...
public function resource()
{
return $this->belongsTo('App\Models\Resource');
}
public function location()
{
return $this->belongsTo('App\Models\Location');
}
}
Ressourcenmodell
/**
* 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');
}
}
<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>
Ich wollte nur eine Spalte oder zwei zu sehen, hinzuzufügen, wenn es funktioniert, aber ich halte eine undefinierte Variable auf resourcelocations
immer noch versucht, meinen Kopf um Laravel zu wickeln und wie die Beziehungen arbeiten, so vielleicht ist meine Logik durcheinander. Jede Hilfe wäre großartig!
Danke.
Danke. Ich wusste nicht, dass Sie die beiden Tabellen verbinden können, indem Sie nur diese Anweisung verwenden. Ich dachte an SQL-Join-Anweisungen. ** EDIT ** Alles klar, die Namensräume waren anders. Fortschritt, ich sehe die Tabellen, nur keine Daten. – ph0bolus
Die Funktion 'belongsToMany' übernimmt zusätzliche Parameter für die IDs. Wenn Sie anders sind als Laravel erwartet, gibt es wahrscheinlich keine Daten zurück. Wie heißen Ihre IDs in Ihrer 'ResourceLocation' Tabelle? – user3158900
Entschuldigung, ich habe es herausgefunden. anstelle von '{{$ resource-> name}}' war es 'Name' in meiner Datenbank, also war alles leer. Nachdem ich sie alle groß geschrieben habe, zeigten die Daten korrekt. Danke noch einmal! – ph0bolus