我正在建立一个论坛,用户可以在这个论坛中创建主题并留下回复。
我建立了一个如下所示的关系。但是,当我保存文章时,topic_id没有附加。我认为saveReply方法是错误的。
此外,在这种情况下,如何将特定帖子的注释传递给show方法中的视图?
我是一个菜鸟,所以如果我的问题含糊不清,我很抱歉,但任何帮助都将不胜感激!!
路线
Route::group(['middleware' => 'web'], function () {
Route::get('forums','ForumsController@index');
Route::get('forums/create','ForumsController@create');
Route::post('forums', 'ForumsController@store');
Route::get('forums/{category_id}/{title}','ForumsController@show');
Route::post('forums/{category_id}/{title}', 'ForumsController@saveReply');
});

forumcontroller
class ForumsController extends Controller
{
public function index()
{
$categories = Category::all();
$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 = [
'title',
'body',
'category_id'
];
public function category()
{
return $this->belongsTo('App\category');
}
public function user()
{
return $this->belongsTo('App\User');
}
public function comments()
{
return $this->hasMany('App\Comment');
}
}
&#13;
用户模型
<?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');
}
}
&#13;
评论模型
class Comment extends Model
{
protected $fillable = [
'reply',
'user_id',
'topic_id'
];
public function topic()
{
return $this->belongsTo('App\Topic');
}
public function user()
{
return $this->belongsTo('App\User');
}
}
&#13;
评论表
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');
}
}
&#13;
答案 0 :(得分: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,这样您就可以获得有意义的错误消息,以便轻松找到exect问题。