2016-06-13 7 views
0

私は小さなフォーラムを持っていますが、私はストアメソッドのトピックと返信を作成しようとしています。Sentinelを使用してトピックモデルをデータベースに格納する際にエラーが発生しました

Route::get('board/{id}/create', '[email protected]'); 
Route::post('board/{id}/create', '[email protected]'); 

routes.phpの

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

私はこのエラーが発生します。

Call to a member function topics() on null 

はまた、私は、このレポからセンチネルhttps://github.com/rydurham/Sentinelを使用しています、注意してください。モデル

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

ルートを表示できますか? – geckob

+0

ユーザーを取得するには暗黙的なバインドを使用する必要があります。今はNULLです – geckob

+0

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

答えて

1

を更新しました

<?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"; 
    } 
} 

あなたのユーザーオブジェクトがnullであると思われます。 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

モデルにはApp \ Modelsという名前空間があり、senitelを使用しています。これにはラッパーが必要です。Sentry :: getUser – manshu

+0

@manshu:はい、考えましたが、 – geckob

+0

は動作しません。 https://github.com/rydurham/Sentinel/issues/37これをチェックしてください。 – manshu

関連する問題