php - Eloquent Query Scope return Builder instead of Model when using phpunit
I have the following code
$user = User::findByAccountCode($transaction->account_code);
When I execute this code on phpunit it returns an instance of Illuminate\Database\Eloquent\Builder instead of User Model.
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: Argument 1 passed to aunicaj\Libraries\MarkupRepository::user() must be an instance of aunicaj\Models\User, instance of Illuminate\Database\Eloquent\Builder given
When I use the browser its working fine but not on phpunit. Thank you
Answer
Solution:
You're using query scopes incorrectly. They should never fetch any records (this is what your call to first() is doing) - they are only allowed to update the query with adding/removing constraints.
Replace
with
and use it everywhere like the following:
If you want to have a method in your User method that would return a user for given account code, feel free to create it, but don't start its name with scope, e.g.:
This way your code will work as you wanted - call the following to get a single user:
Answer
Solution:
I got the problem solve turns out that my factory method in my test is using
it should be