2016-02-26 128 views
1

我正在創建一個論壇,用戶可以在該論壇中創建主題並留下回復,就像本論壇一樣。無法在評論表中添加topic_id

我做了一個像下面的關係。但是,當我保存一篇文章topic_id沒有被連接。我認爲saveReply方法是錯誤的。

另外,在這種情況下,您如何將特定帖子的評論傳遞給show方法中的視圖?

我是一個noob,所以如果我的問題含糊不清,但任何幫助將不勝感激!

路線

Route::group(['middleware' => 'web'], function() { 
 
    Route::get('forums','[email protected]'); 
 
    Route::get('forums/create','[email protected]'); 
 
    Route::post('forums', '[email protected]'); 
 
    Route::get('forums/{category_id}/{title}','[email protected]'); 
 
    Route::post('forums/{category_id}/{title}', '[email protected]'); 
 

 
});

forumcontroller

class ForumsController extends Controller 
 
{ 
 
    public function index() 
 
    { 
 
    \t $categories = Category::all(); 
 
    \t $topics = Topic::latest()->get(); 
 
     return view('forums.index',compact('categories','topics')); 
 
    } 
 

 
    public function create() 
 
    { 
 
     $categories = Category::lists('title', 'id'); 
 
     return view('forums.create', compact('categories')); 
 
    } 
 

 
    public function store(Request $request) 
 
    { 
 
     Auth::user()->topics()->save(new Topic($request->all())); 
 

 
     flash()->success('投稿しました','success'); 
 

 
     return redirect('forums'); 
 
    } 
 
     
 

 
    public function show($category_id, $title) 
 
    { 
 
     Topic::where(compact('category_id','title'))->first(); 
 

 
     
 

 

 

 
     return view('forums.post', compact('topic')); 
 
    } 
 

 
    public function saveReply (Request $request) 
 
    { 
 
     Auth::user()->comments()->save(new Comment($category_id,$request->all())); 
 

 

 
     flash()->success('投稿しました','success'); 
 

 
     return redirect()->back(); 
 
    } 
 
}

主題模型

<?php 
 

 
namespace App; 
 

 
use Illuminate\Database\Eloquent\Model; 
 

 
class topic extends Model 
 
{ 
 
    protected $fillable = [ 
 
    \t 'title', 
 
    \t 'body', 
 
     'category_id' 
 
    \t ]; 
 

 
    public function category() 
 
    { 
 
     return $this->belongsTo('App\category'); 
 
    } 
 

 
    public function user() 
 
    { 
 
     return $this->belongsTo('App\User'); 
 
    } 
 

 
    public function comments() 
 
    { 
 
     return $this->hasMany('App\Comment'); 
 
    } 
 
}

用戶模型

<?php 
 

 
namespace App; 
 

 
use Illuminate\Foundation\Auth\User as Authenticatable; 
 

 
class User extends Authenticatable 
 
{ 
 
    /** 
 
    * The attributes that are mass assignable. 
 
    * 
 
    * @var array 
 
    */ 
 
    protected $fillable = [ 
 
     'name', 'email', 'password', 
 
    ]; 
 

 
    /** 
 
    * The attributes excluded from the model's JSON form. 
 
    * 
 
    * @var array 
 
    */ 
 
    protected $hidden = [ 
 
     'password', 'remember_token', 
 
    ]; 
 

 
    public function articles() 
 
    { 
 
     return $this->hasMany('App\Article'); 
 
    } 
 

 
    public function topics() 
 
    { 
 
     return $this->hasMany('App\Topic'); 
 
    } 
 

 
    public function comments() 
 
    { 
 
     return $this->hasMany('App\Comment'); 
 
    } 
 
}

評論模型

class Comment extends Model 
 
{ 
 
\t protected $fillable = [ 
 
\t \t \t 'reply', 
 
\t \t \t 'user_id', 
 
\t \t \t 'topic_id' 
 
\t \t ]; 
 

 

 
    public function topic() 
 
    { 
 
     return $this->belongsTo('App\Topic'); 
 
    } 
 

 
    public function user() 
 
    { 
 
     return $this->belongsTo('App\User'); 
 
    } 
 
}

評語表

class CreateCommentsTable extends Migration 
 
{ 
 
     public function up() 
 
    { 
 
     Schema::create('comments', function (Blueprint $table) { 
 
      $table->increments('id'); 
 
      $table->text('reply'); 
 
      $table->integer('user_id')->unsigned(); 
 

 
      $table->integer('topic_id')->unsigned(); 
 
      $table->timestamps(); 
 
     }); 
 
    } 
 

 
    
 
    public function down() 
 
    { 
 
     Schema::drop('comments'); 
 
    } 
 
}

回答

0

Request::all返回的所有輸入數組,所以當你正在做的:

new Comment($category_id,$request->all()) 

你會得到這樣的事情:

1['some' => 'thing', 'other'=> 'values'] 

這可能是問題,以便試試這個來代替:

new Comment(array_merge(['category_id' => $category_id ], $request->all()) 

當開發/當地環境,設置debug true,所以你會得到有意義的錯誤信息,所以你可以很容易地findout的exect問題。