Question: I have custom Request class:

```php
class ApiRequest extends FormRequest
{
    ...
    public function locale()
    {
        $lang = $this->header('Accept-Language');
        return $lang ? $lang : 'uz';
    }
}

```

and API recourse:

```php
class SomeResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            ...
        ];
    }
}

```

but I can't use ApiRequest's method `locale()` in SomeResource. Because `toArray()` accepts only `\Illuminate\Http\Request`. 

Is there any idea that I pass my class into this? Or a better solution from you.

Answer: 
The request instance bound in the container is injected into the JSON resource class when the response is created.

One method to achieve what you want would be to create your custom request that extends the standard request class and then bind that in the container as the request instance to be used for the remainder of the request. This could be done in a middleware assigned specifically to the API group or just in specific controllers.

This will then be injected into the `toArray()` method in your JSON resource class when the response is built and your custom methods will be available to use.

**Custom Request Class**

```
class ApiRequest extends Request
{
    ...
    public function locale()
    {
        $lang = $this->header('Accept-Language');
        return $lang ? $lang : 'uz';
    }
}

```

**API Middleware**

```
class ApiMiddleware
{
    ...
    public function handle($request, $next)
    {
        $request = app()->instance('request', ApiRequest::createFrom($request));

        return $next($request);
    }
}

```

**JSON Resource**

```
class SomeResource extends JsonResource
{
    public function toArray($request)
    {
        // $request is now instanceof ApiRequest

        return [
            'id' => $this->id,
            'locale' => $request->locale()
        ];
    }
}

```
[API Resources](https://laravel.com/docs/5.7/eloquent-resources) should receive model instances not [Requests](https://laravel.com/docs/5.7/requests) itself. This is an example:

**ApiRequest.php**

```
ApiRequest extends Request {

    public function authorize() { /** your logic */ }

    public function rules() { /** your logic */ }

}

```

**MyCoolController.php**

```
MyCoolController extends Controller {

    public function myFunction(ApiRequest $request)
    {
        $lang = $request->header('Accept-Language') ?? 'uz';
        \App::setLocale($lang); // <--

        $model = MyModel::find($request->get('id'));

        return new SomeResource($model);
    }
}

```