第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Laravel 測試中對 null 調(diào)用成員函數(shù) save()

在 Laravel 測試中對 null 調(diào)用成員函數(shù) save()

PHP
汪汪一只貓 2023-07-01 15:37:11
到目前為止我有一個類別模型:class Category extends Model{? ?? ? protected $fillable = [? ? ? ? 'name',? ? ? ? 'slug',? ? ? ? 'order'? ? ];? ? public function scopeParents(Builder $builder){? ? ? ? $builder->whereNull('parent_id');? ? }? ? public function scopeOrder(Builder $builder, $direction = 'asc'){? ? ? ? $builder->orderBy('order', $direction);? ? }? ? public function children(){? ? ? ? $this->hasMany(Category::class, 'parent_id', 'id');? ? }}一個工廠:$factory->define(Category::class, function (Faker $faker) {? ? return [? ? ? ? 'name' => $name = $faker->unique()->name,? ? ? ? 'slug' => Str::slug($name)? ? ];});還有一個測試public function test_it_has_many_children()? ? {? ? ? ? $category = factory(Category::class)->create();? ? ? ? $category->children()->save(? ? ? ? ? ? factory(Category::class)->create()? ? ? ? );? ? ? ? $this->assertInstanceOf(Category::class, $category->children->first());? ? }但是,當(dāng)我運行測試時,我得到:Call to a member function save() on null? at tests/Unit/Models/Categories/CategoryTest.php:14? ? 10|? ? ?public function test_it_many_children()? ? 11|? ? ?{? ? 12|? ? ? ? ?$category = factory(Category::class)->create();? ? 13|?? > 14|? ? ? ? ?$category->children()->save(? ? 15|? ? ? ? ? ? ?factory(Category::class)->create()? ? 16|? ? ? ? ?);? ? 17|?? ? 18|? ? ? ? ?$this->assertInstanceOf(Category::class, $category->children->first());是什么賦予了?該課程已經(jīng)有幾年的歷史了,所以我認(rèn)為 Laravel 版本之間存在一些差異,但這似乎更基本。
查看完整描述

1 回答

?
慕的地6264312

TA貢獻(xiàn)1817條經(jīng)驗 獲得超6個贊

您的關(guān)系方法中缺少返回:

public function children(){
    return $this->hasMany(Category::class, 'parent_id', 'id');
}


查看完整回答
反對 回復(fù) 2023-07-01
  • 1 回答
  • 0 關(guān)注
  • 126 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號