以一对多多态关系存储到特定ID

时间:2019-08-29 16:17:49

标签: laravel eloquent

我有以下3种型号:

指南:

class Guide extends Model
{
    protected $guarded = [];

    public function commentable()
    {
        return $this->morphTo();
    }
}

游戏:

class Game extends Model
{
    public function guides(){
        return $this->morphMany(Guide::class, 'commentable')->orderBy('created_at', 'DESC');
    }
}

用户:

class User extends Authenticatable
{
    public function guides(){
        return $this->morphMany(Guide::class, 'commentable')->orderBy('created_at', 'DESC');
    }
}

以及以下控制器:

GuidesContoller:

class GuidesContoller extends Controller
{
public function store()
    {
        $data = request()->validate([
            'game_id' => 'required',
            'title' => 'required',
            'description' => 'required',
        ]);

        $guide= auth()->user()->guides()->create($data);

        return redirect("/guide/{$guide->id}/{$guide->title}");
    }
}

由于General error: 1 table builds has no column named game_id而给出'game_id' => 'required',因为数据库中没有game_id。在创建新指南时,如果登录用户选择特定游戏,我将如何执行此保存操作?

0 个答案:

没有答案