Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifying the Query before Building #41

Open
zlanich opened this issue Feb 18, 2018 · 1 comment
Open

Modifying the Query before Building #41

zlanich opened this issue Feb 18, 2018 · 1 comment

Comments

@zlanich
Copy link

zlanich commented Feb 18, 2018

I'm not seeing an appropriate way to make modifications to the query before calling QueryBuilder::build(). I'd like to add a ->where('user_id', Auth::guard()->user->id) clause onto the resulting where to filter results to only those owned by the currently authenticated user.

How am I supposed to accomplish this?

Thanks!

@caioflavio
Copy link

caioflavio commented Mar 30, 2021

I'm a couple years late, but it can help someone else, you can use Custom Requests to achieve this.

<?php
namespace App\Http\Controllers;

use Unlu\Laravel\Api\QueryBuilder;
use Unlu\Laravel\Api\RequestCreator;

class YourController extends Controller
{
    public function index(Request $request)
    {
        $customRequest = RequestCreator::createWithParameters(
            array_merge($request->all(),  ['your_filter_column' => '=your_filter_value'])
        );
        $queryBuilder = new QueryBuilder(new ActionSuggestionMotor, $customRequest);
        return response()->json([
            'data' => $queryBuilder->build()->paginate()
        ]);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants