我需要你的帮助。我正在学习Laravel,我知道它已被多次询问,但无法使其正常工作。我正在尝试使用page_id.我store在CategoryController中的方法如下所示:public function store(Page $page){ $data = request()->validate([ 'title' => 'required' ]); $category = $page->categories()->create($data); return redirect('/category/' . $category->id);}我的页面model如下所示:<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Page extends Model{ protected $guarded = []; public function categories() { $this->hasMany(Category::class); }}我的类别migration如下所示: public function up() { Schema::create('categories', function (Blueprint $table) { $table->bigIncrements('id'); $table->unsignedBigInteger('page_id'); $table->string('title'); $table->timestamps(); }); }我想先了解我做错了什么。
1 回答
皈依舞
TA贡献1851条经验 获得超3个赞
您的类别关系必须返回 hasMany 对象。将代码更改为:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Page extends Model
{
protected $guarded = [];
public function categories() {
return $this->hasMany(Category::class);
}
}
- 1 回答
- 0 关注
- 101 浏览
添加回答
举报
0/150
提交
取消