php - Eloquent Query Scope return Builder instead of Model when using phpunit

567

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

506

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

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);
203

Answer

Solution:

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

People are also looking for solutions to the problem: php - How to send client access token with app secret to Facebook account kit and get the resons

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.