Laravel:“强制”查询生成器返回空列表

我有以下路线:


Route::apiResource('payments', 'PaymentController', ['except' => ['store', 'destroy']]);


Route::get('users/{user}/payments', [

    'as' => 'users.payments',

    'uses' => 'PaymentController@index',

]);

这个控制器:


class PaymentController

{

    public function index(Request $request, User $user)

    {

        // Initialize query builder

        $query = Payment::query();


        // If a user was provided, return all payments belonging to that user

        // if condition is satisfied

        if ($user) {

            if (condition) {

                $query = $customer->payments();

            } else {

                // Some code that causes no results

                $query->whereNull('id');   // <----- This hits the database

            }

        }


        return PaymentResource::collection($query->paginate(10));

    }

}

如果您点击它,则应返回所有用户进行的所有付款。/payments


如果命中它,则仅当为 true 时,它才应返回用户进行的所有付款,否则应返回空列表。/users/id/paymentscondition


此代码有效,但是是实际命中数据库的解决方法。$query->whereNull('id');


有没有办法避免命中数据库并仍然返回空列表?


30秒到达战场
浏览 89回答 2
2回答

猛跑小猪

您可以避免命中数据库,但仍然使用初始化的变量返回空列表到空集合或数组class PaymentController{&nbsp; &nbsp; public function index(Request $request, User $user)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Initialize query builder&nbsp; &nbsp; &nbsp; &nbsp; $query = Payment::query();&nbsp; &nbsp; &nbsp; &nbsp; // If a user was provided, return all payments belonging to that user&nbsp; &nbsp; &nbsp; &nbsp; // if condition is satisfied&nbsp; &nbsp; &nbsp; &nbsp; $return = collect();&nbsp; &nbsp; &nbsp; &nbsp; if ($user) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (condition) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $return = $customer->payments()->paginate(10);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return PaymentResource::collection($return);&nbsp; &nbsp; }}

缥缈止盈

我试图在函数中有一个单一的返回,但更容易遵循@Chin梁给出的建议(谢谢!class PaymentController{&nbsp; &nbsp; public function index(Request $request, User $user)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Initialize query builder&nbsp; &nbsp; &nbsp; &nbsp; $query = Payment::query();&nbsp; &nbsp; &nbsp; &nbsp; // If a user was provided, return all payments belonging to that user&nbsp; &nbsp; &nbsp; &nbsp; // if condition is satisfied&nbsp; &nbsp; &nbsp; &nbsp; if ($user) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (condition) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query = $customer->payments();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Some code that causes no results&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return PaymentResource::collection(collect());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Other code here, for example a where statement&nbsp; &nbsp; &nbsp; &nbsp; // depending on what is passed in $request&nbsp; &nbsp; &nbsp; &nbsp; return PaymentResource::collection($query->paginate(10));&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP