无法使用laravel雄辩获取数据

What I want is that when a user visits this link /api/bonus?provider[]=MyCompany the result will show only bonus provided by providers=[MyCompany].

In my controller:

public function all(Request $request)
{
    $size = $request->input('size');

    $bonus = Bonus::with('category', 'bonus');
    $bonus = Filterer::apply($request, $size, $bonus);

 $bonus = Filterer::apply($request, $size, $bonus);

        return response()->json([
            'code' => 0,
            'success' => true,
            'data' => BonusResource::collection($bonus),

}

My expected result is to get all the providers that equal the [MyCompany] But somehow this query doesn't work.

Filterer

public static function apply(Request $filters, $size, $bonus)
{
     if ($filters->has('provider')) {
                $bonus->whereHas('bonus', function ($query) use ($filters) {
                    $query->whereIn('providers', $filters->input('provider',[]));

                });
            }
return $bonus->paginate($size);
}

but at the end I'm getting this result. The data is null [].

enter image description here

I'm wonder why I can't get the data. Which part I had done wrong?

I might be wrong, but based on your code, the function apply didn't return anything, it will always empty. Shouldn't it be return $bonus? and don't forget the get.

public static function apply(Request $filters, $size, $bonus)
{
     if ($filters->has('provider')) {
         $bonus->whereHas('bonusRelease', function ($bonus) use ($filters) {
             return $bonus->whereJsonContains('providers', $filters->input('provider',[]));
         });
     }
     return $bonus->get();
}

Updated my answer

The providers is a json type. You can't just query it with where in (a,b,c). Change to whereJsonContains. Please check this link another case and laravel docs.

To actually get results you should use some function that gets data from database, for example get, first (depends whether you want to get multiple rows or only first row). Now you only prepare query that should be executed but you never execute it, so yo should change line:

$bonus = Filterer::apply($request, $size, $bonus);

into

$bonus = Filterer::apply($request, $size, $bonus);
$bonus = $bonus->get();

to execute query and get results.

First, convert the input into array, here I'm using explode() and then I loop the converted array and use %% to search the providers. Hope it will help!

if ($filters->has('provider') && trim($filters->input('provider')) != "") {
            $bonus_list->whereHas('bonusCompany', function ($query) use ($filters) {

                $providers = explode(',', (trim($filters->input('provider'), '[]')));

                foreach ($providers as $key => $provider) {
                    if ($key == 0) {
                        $query->where('providers', 'LIKE', ['%' . trim($provider) . '%']);
                    } else {
                        $query->orwhere('providers', 'LIKE', ['%' . trim($provider) . '%']);
                    }
                }
            });
        };