2016-06-13 13 views
0

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)); 
    } 
+0

Kannst du deine Route anzeigen? – geckob

+0

Sie sollten implizite Bindung verwenden, um den Benutzer zu erhalten. Jetzt ist es null – geckob

+0

Route :: get ('board/{id}/create', 'ThemenController @ create'); Route :: post ('board/{id}/create', 'TopicsController @ store'); – manshu

Antwort

1

Es scheint, dass Ihr Benutzerobjekt null ist. Reichen Sie den Benutzer ordnungsgemäß mit der ID

public function store($id) 
{ 

    $user = \App\Models\User::find(\Sentinel::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)); 
} 
+0

Modell hat Namespace App \ Models und ich benutze Senitel, das erfordert einen Wrapper Sentry :: getUser – manshu

+0

@manshu: Ja, aber Sie haben die Idee – geckob

+0

Nein, es funktioniert nicht. https://github.com/rydurham/Sentinel/issues/37 checken Sie das aus. – manshu

Verwandte Themen