雄辩的查询范围返回生成器,而不是使用phpunit时的模型 [英] Eloquent Query Scope return Builder instead of Model when using phpunit

查看:66
本文介绍了雄辩的查询范围返回生成器,而不是使用phpunit时的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

$user = User::findByAccountCode($transaction->account_code);

当我在phpunit上执行此代码时,它将返回Illuminate \ Database \ Eloquent \ Builder的实例,而不是用户模型.

When I execute this code on phpunit it returns an instance of Illuminate\Database\Eloquent\Builder instead of User Model.

这是findByAccountCode的代码

Here is the code for findByAccountCode

public function scopeFindByAccountCode($query,$account_code){


   return $query->where('account_code', $account_code)->first();

}

我的应用程序出现以下错误

I get the following error on my Application

ErrorException:参数1传递给 aunicaj \ Libraries \ MarkupRepository :: user()必须是以下对象的实例 aunicaj \ Models \ User,Illuminate \ Database \ Eloquent \ Builder的实例 给

ErrorException: Argument 1 passed to aunicaj\Libraries\MarkupRepository::user() must be an instance of aunicaj\Models\User, instance of Illuminate\Database\Eloquent\Builder given

当我使用浏览器时,它可以正常运行,但不能在phpunit上运行.谢谢

When I use the browser its working fine but not on phpunit. Thank you

推荐答案

我解决了问题,结果发现我测试中的工厂方法正在使用

I got the problem solve turns out that my factory method in my test is using

factory(User::class)->make() 

应该是

factory(User::class)->create()

这篇关于雄辩的查询范围返回生成器,而不是使用phpunit时的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆