Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel错误:调用null上的成员函数create()_Php_Laravel_Eloquent - Fatal编程技术网

Php Laravel错误:调用null上的成员函数create()

Php Laravel错误:调用null上的成员函数create(),php,laravel,eloquent,Php,Laravel,Eloquent,我需要你的帮助。我正在学习Laravel,我知道它已经被问过很多次了,但是我不能让它工作 我正试图用页面id保存一个类别 我在CategoryController中的存储方法如下所示: public function store(Page $page){ $data = request()->validate([ 'title' => 'required' ]); $category = $page->categories()->

我需要你的帮助。我正在学习Laravel,我知道它已经被问过很多次了,但是我不能让它工作

我正试图用
页面id
保存一个类别

我在CategoryController中的
存储
方法如下所示:

public function store(Page $page){

    $data = request()->validate([
        'title' => 'required'
    ]);

    $category = $page->categories()->create($data);

    return redirect('/category/' . $category->id);
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

    public function categories() {
        $this->hasMany(Category::class);
    }
}

 public function up()
   {
        Schema::create('categories', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('page_id');
            $table->string('title');
            $table->timestamps();
        });
   }

我的页面
model
如下所示:

public function store(Page $page){

    $data = request()->validate([
        'title' => 'required'
    ]);

    $category = $page->categories()->create($data);

    return redirect('/category/' . $category->id);
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

    public function categories() {
        $this->hasMany(Category::class);
    }
}

 public function up()
   {
        Schema::create('categories', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('page_id');
            $table->string('title');
            $table->timestamps();
        });
   }

我想首先了解我做错了什么

谢谢,,
Cheers

您的类别关系必须返回hasMany对象。 将代码更改为:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

    public function categories() {
        return $this->hasMany(Category::class);
    }
}

您的categories关系必须返回hasMany对象。
将代码更改为:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

    public function categories() {
        return $this->hasMany(Category::class);
    }
}

您发布的代码不应该抛出该错误
$page->categories()
,如果配置正确,应该返回一个
关系
,而
hasMany()
不应该返回
null
(它可以返回一个空的,但这不是
null
)。执行
dd($page->categories())
并查看返回的内容。您发布的代码不应抛出该错误
$page->categories()
,如果配置正确,应该返回一个
关系
,而
hasMany()
不应该返回
null
(它可以返回一个空的,但这不是
null
)。执行
dd($page->categories())
并查看返回的内容。谢谢,它缺少return关键字。它正在工作。谢谢,它缺少return关键字。它现在正在工作。