自定义查询功能laravel

I often need to perform this query:

$Op = JobCardOp::where([
        ['JobCardNum', '=', $JobCardNum ],
        ['OpNum', '=', $OpNum ]
        ])->first();

So rather than writing this out every time I want a function like:

 public function getOp($JobCardNum, $OpNum)
{
    $Op = JobCardOp::where([
        ['JobCardNum', '=', $JobCardNum ],
        ['OpNum', '=', $OpNum ]
        ])->first();
    return $Op;
}

That I can call in my controller. Where should I define my function, at the moment the I only need it in one controller but I may need it an another if thats possible. Any help appreciated.

You could put this method on your Model if you wanted to as a static function.

public static function getOp($cardNum, $opNum)
{
    return static::where([
        ['JobCardNum', '=', $cardNum],
        ['OpNum', '=', $opNum]
    ])->first();
}

// controller

$res = YourModel::getOp($cardNum, $opNum);

Or add a query scope to the model

public function scopeGetOp($query, $cardNum, $opNum)
{
    return $query->where([
        ['JobCardNum', '=', $cardNum],
        ['OpNum', '=', $opNum]
    ]);
}

// controller

$res = YourModel::with(...)->getOp($cardNum, $opNum)->first();

Kinda depends how you want to use it.

You may define your function in JobCardOpt model as static:

public static function getOp($JobCardNum, $OpNum)
{
    $Op = static::where([
        ['JobCardNum', '=', $JobCardNum],
        ['OpNum', '=', $OpNum]
    ])->first();
    return $Op;
}

And use it like this in your controllers:

$jobCardOpt = JobCardOpt::getOp(1, 2);