Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 235
iamkristher Avatar asked Jan 05 '23 13:01

iamkristher


2 Answers

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

public function scopeFindByAccountCode($query,$account_code){
  return $query->where('account_code', $account_code)->first();
}

with

public function scopeFindByAccountCode($query,$account_code){
  return $query->where('account_code', $account_code);
}

and use it everywhere like the following:

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

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.:

public static function findByAccountCode($account_code){
  return static::where('account_code', $account_code)->first();
}

This way your code will work as you wanted - call the following to get a single user:

$user = User::findByAccountCode($transaction->account_code);
like image 58
jedrzej.kurylo Avatar answered Jan 21 '23 16:01

jedrzej.kurylo


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

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

it should be

factory(User::class)->create()
like image 27
iamkristher Avatar answered Jan 21 '23 16:01

iamkristher