Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use apiReources method with `only`?

I'm creating an api with Laravel and I am looking for an easy lazy way to to register Api resources. I'm currently defining my routes like this:

Route::apiResource('categories', 'CategoryController')->only(['index', 'show']);

I checked Laravel's controller documentation and I saw apiResources method which I can create multiple api resources at once.

the goal: is to be able to use apiResources with only method like this

Route::apiResources(['categories' => 'CategoryController', 'products' => 'ProductController'])->only(['index', 'show']);

current result:

Call to a member function only() on null

like image 208
Roduan Avatar asked May 30 '20 08:05

Roduan


People also ask

What is Laravel API resources?

Laravel's resource classes allow you to expressively and easily transform your models and model collections into JSON. Basically you can generate a nice json formatted data right from Eloquent.

How do you make a resource controller in Laravel?

Creating the Controller This is the easy part. From the command line in the root directory of your Laravel project, type: php artisan make:controller sharkController --resource This will create our resource controller with all the methods we need.

What is API php in Laravel?

routes/api. php file is used when you want to make a Restful API . It means that you are developing the front end of your project with something else(for example: angular) and you want to develope your back end using Laravel. In that case your all routes will go in routes/api. php .

What is the use of resource in Laravel?

A resource controller is used to create a controller that handles all the http requests stored by your application. The resource() is a static function like get() method that gives access to multiple routes that we can use in a controller. Syntax of resource() method: Route::resource('posts','PostController');


1 Answers

long story short (if you don't want to read the whole story) you can just do it like this:

Route::apiResources(['brands' => 'BrandController', 'categories' => 'CategoryController'], ['only' => ['index', 'show']]);

When I was writing the question it passed to my mind to check the apiResources declaration and I found this:

   /**
     * Register an array of API resource controllers.
     *
     * @param  array  $resources
     * @param  array  $options
     * @return void
     */
    public function apiResources(array $resources, array $options = [])
    {
        foreach ($resources as $name => $controller) {
            $this->apiResource($name, $controller, $options);
        }
    }

and since it is using apiResource under the hood and it is passing options parameter I can check what are these options

/**
 * Route an API resource to a controller.
 *
 * @param  string  $name
 * @param  string  $controller
 * @param  array  $options
 * @return \Illuminate\Routing\PendingResourceRegistration
 */
public function apiResource($name, $controller, array $options = [])
{
    $only = ['index', 'show', 'store', 'update', 'destroy'];

    if (isset($options['except'])) {
        $only = array_diff($only, (array) $options['except']);
    }

    return $this->resource($name, $controller, array_merge([
        'only' => $only,
    ], $options));
}
like image 199
Roduan Avatar answered Oct 27 '22 11:10

Roduan