Ich habe ein kleines Forum, ich versuche, Thema und Antworten für die Speichermethode zu erstellen.Fehler beim Speichern des Themenmodells in der Datenbank mit Sentinel
routes.php
Route::get('board/{id}/create', '[email protected]');
Route::post('board/{id}/create', '[email protected]');
TopicsController.php
public function store()
{
$this->request->user()->topics()->create([
'board_id' => $this->request->id,
'title' => $this->request->title,
'body' => $this->request->body
]);
return redirect(url('/board/' . $this->request->id));
}
ich diesen Fehler erhalte.
Call to a member function topics() on null
Beachten Sie auch, ich bin mit Sentinel https://github.com/rydurham/Sentinel aus diesem Repo.
<?php namespace App\Models;
class User extends \Sentinel\Models\User
{
protected $fillable = ['email', 'first_name', 'last_name'];
protected $hidden = ['password'];
public function topics()
{
return $this->hasMany(Topic::class);
}
public function replies()
{
return $this->hasMany(Reply::class);
}
public function getGravatarAttribute()
{
$hash = md5(strtolower(trim($this->attributes['email'])));
return "https://www.gravatar.com/avatar/$hash";
}
}
aktualisiert Modell
public function store($id)
{
$user = Sentry::getUser($id);
$user->topics()->create([
'board_id' => $this->request->id,
'title' => $this->request->title,
'body' => $this->request->body
]);
return redirect(url('/board/' . $this->request->id));
}
Kannst du deine Route anzeigen? – geckob
Sie sollten implizite Bindung verwenden, um den Benutzer zu erhalten. Jetzt ist es null – geckob
Route :: get ('board/{id}/create', 'ThemenController @ create'); Route :: post ('board/{id}/create', 'TopicsController @ store'); – manshu