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: 
I created a base Resource class, that all my other resource classes extend it with my custom method:

```
class BaseResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return parent::toArray($request);
    }

    protected function locale($request)
    {
        $lang = $request->header('Accept-Language');
        return $lang ? $lang : 'uz';
    }
}

```

and I use it in child class:

```
class SomeResource extends BaseResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $lang = $this->locale($request);

        return [
            'id' => $this->id,
            'hash' => $this->hash,
            'name'  => $this->translation('name', $this->locale($request)),
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}

```
[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);
    }
}

```