0

I'm trying to build a query from a Repository in a Model with 2 where clauses.

This is the data I have in a MySql table:

id      name            environment_hash
1       online_debit    abc
2       credit_cart     abc

I want to query by name and environment_hash. To do this, I created the method findByHashAndMethod() (see below).

But when I use it in my controller, like this:

$online_debit = $this->ecommercePaymentMethodRepository->findByHashAndMethod($hash, 'online_debit')->first();

or this:

$credit_card = $this->ecommercePaymentMethodRepository->findByHashAndMethod($hash, 'credit_cart')->first();

I keep getting both rows and not only the ones filtered. What's wrong with the code?

This is my PaymentMethodRepository.php

class EcommercePaymentMethodRepository extends BaseRepository
{


    public function findByHashAndMethod($hash = null, $payment_method)
    {
        $model = $this->model;

        if($hash) 
        {
            $filters = ['environment_hash' => $hash, 'name' => $payment_method];
            $this->model->where($filters);
        }

        else
        {
            $this->model->where('environment_hash',  Auth::user()->environment_hash)
                        ->where('name', $payment_method);
        }


        return $model;
    }

    public function model()
    {
        return EcommercePaymentMethod::class;
    }
}

And this is my model EcommercePaymentMethod.php

<?php

namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class EcommercePaymentMethod extends Model
{
    use SoftDeletes;

    public $table = "ecommerce_payment_methods";

    protected $dates = ['deleted_at'];

    public $fillable = [
        "name",
        "payment_processor_id",
        "active",
        "environment_hash"
    ];

    protected $casts = [
        "name" => "string"
    ];

    public function payment_processor()
    {
        return $this->hasOne('App\Models\EcommercePaymentProcessor');
    }
}
2
  • try to add ->get() when returning Commented Oct 17, 2016 at 10:30
  • You're mixing $this->model with $model in your method. Commented Oct 17, 2016 at 10:34

1 Answer 1

2

While I am not entirely sure why ->first() would ever return more than one result, your Repository method had some few glaring issues that's prone to errors.

class EcommercePaymentMethodRepository extends BaseRepository
{

    // 1. Do not put optional parameter BEFORE non-optional
    public function findByHashAndMethod($payment_method, $hash = null)
    {
        // 2. Call ->model() method
        $model = new $this->model();

        // 3. Logic cleanup
        if (is_null($hash)) {
            $hash = Auth::user()->environment_hash;
        }

        return $model->where('environment_hash', $hash)
            ->where('name', $payment_method);
    }

    public function model()
    {
        return EcommercePaymentMethod::class;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.