我需要你的幫助。我正在學習Laravel,我知道它已經(jīng)被問過很多次了,但不能讓它起作用。我正在嘗試使用 .page_id我在 CategoryController 中的方法如下所示:storepublic 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貢獻1836條經(jīng)驗 獲得超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 關注
- 81 瀏覽
添加回答
舉報
0/150
提交
取消