我需要你的幫助。我正在學習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 關注
- 130 瀏覽
添加回答
舉報
0/150
提交
取消