[英]Laravel 5.1 testing with model relationships and factories
目前,我有2個模型(和測試工廠)用於:
我看過有關具有關系的測試工廠的文檔,但是沒有針對單個實例專門顯示任何內容。 我現在所擁有的是:
$user = factory(App\\User::class)->create(); $post = factory(App\\Post::class)->create(); $post->user()->save($user);
我當前收到以下PHPUnit錯誤: BadMethodCallException: Call to undefined method Illuminate\\Database\\Query\\Builder::save()
我可能在這里想念一些小東西。 我該如何工作?
$ post-> user()返回關系定義,而不是相關對象。 如果需要將用戶與帖子關聯並保存,則需要執行以下操作:
$user = factory(App\User::class)->create();
$post = factory(App\Post::class)->create();
$post->user()->associate($user);
$post->save();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.