laravel-api-query-builder icon indicating copy to clipboard operation
laravel-api-query-builder copied to clipboard

Modifying the Query before Building

Open zlanich opened this issue 7 years ago • 1 comments

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!

zlanich avatar Feb 18 '18 20:02 zlanich

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

caioflavio avatar Mar 30 '21 20:03 caioflavio