无法在评论表中附加topic_id

问题描述 投票:0回答:1

我正在创建一个论坛,用户可以像这个论坛一样创建主题并留下回复。

我建立了如下所示的关系。但是,当我保存文章时,

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

论坛管理员

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

用户模型

<?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
{
    protected $fillable = ['reply', 'user_id', 'topic_id'];

    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');
    }
}
php laravel eloquent
1个回答
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,这样您将收到有意义的错误消息,以便您可以轻松找出问题所在。

© www.soinside.com 2019 - 2024. All rights reserved.