Ich versuche, eine Ressource-Provider-Datenbank-Webanwendung mit einer Ressourcen-, Standort-, ResourceLocation (Pivot-Tabelle) und ContactPerson-Modelle einzurichten. Ich bin mir ziemlich sicher, dass ich die Model-Beziehungen richtig eingerichtet habe, weil es von meinem Create A New Resource-Formular die Daten in die Datenbank einfügt. Es wird einfach nicht in meiner Ansicht angezeigt, da die Fremdschlüssel (Resource_ID & Location_ID) nicht vorhanden sind. t in die Pivot-Tabelle eingefügt. Hier ist der Code, den ich bisher habe.Einfügen von zwei Fremdschlüsseln in eine Pivot-Tabelle nach Formularübergabe unter Verwendung von Laravel
Modelle
class Location extends Model
{
public function resource()
{
return $this->belongsToMany('App\Models\Resource', 'ResourceLocation');
}
}
class Resource extends Model
{
public function locations()
{
return $this->belongsToMany('App\Models\Location', 'ResourceLocation');
}
}
class ResourceLocation extends Model
{
protected $table = 'ResourceLocation';
public $timestamps = false;
protected $fillable = [
'Location_ID',
'Resource_ID'
];
}
Resource Controller
public function newResource(CreateNewResourceRequest $req)
{
$resource = Resource::create(Request::only(
\t 'Name',
\t 'Description',
\t 'Misc_Info'
));
$location = Location::create(Request::only(
\t 'Address',
\t 'Address2',
\t 'City',
\t 'Zip_Code',
\t 'County',
\t 'Hours',
\t 'Appt_Necessary'
));
\t $resource->save();
\t \t
\t $resource->location()->attach($location);
\Session::flash('flash_message', 'Resource Created Successfully!');
return redirect('resource');
}
Sobald ich den Knopf auf meinem Formular getroffen bekomme ich den Fehler:
BadMethodCallException in Builder.php line 2345: Call to undefined method Illuminate\Database\Query\Builder::location()
Alle Eingaben von meiner Form in meine Datenbank-Tabellen eingefügt werden, aber die ResourceLocation (Pivot-Tabelle) ist leer.
Wenn ich $resource->$location()->attach($location['Location_ID']);
mache gibt es mir einen Method must be a string
Fehler. Was mache ich hier falsch? Jede Hilfe würde sehr geschätzt werden, danke!