Eloquent Query Scope return Builder instead of Mod

2019-07-11 04:04发布

问题:

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

回答1:

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 scopeFindByAccountCode($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);


回答2:

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()