selahattinunlu / laravel-api-query-builder

Laravel & Lumen Api Query Builder Package
332 stars 66 forks source link

Modifying the Query before Building #41

Open zlanich opened 6 years ago

zlanich commented 6 years ago

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 commented 3 years ago

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()
        ]);
    }
}