Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shaungbhone's avatar

API Resource Routes

In API resource route, I want to group like this

Route::apiResources([
    'photos' => PhotoController::class,
    'posts' => PostController::class,
]);

but I have only methods. How can I put those only methods?

Route::apiResource('news', NewsController::class)->only('index');
Route::apiResource('blogs', PostController::class)->only('index', 'show');
0 likes
4 replies
JohnnyBigodes's avatar

ApiResource provides you already with a CRUD that you need.

If you want to take out a method you could use the ->except() method in the api.php file.

shaungbhone's avatar

@JohnnyBigodes You mean like that

Route::apiResources([
    'photos' => PhotoController::class,
    'posts' => PostController::class,
])->except();
salamaslam.official's avatar

You should define a relation in the Model, or you can use accessors in the Model, here is the example

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DB;

class Project extends Model
{
    use HasFactory;

    protected $fillable = [
        'project_title',
        'permalink',
        'short_description',
        'long_description',
        'categories',
        'featured_image',
        'background_image',
        'created_by',
        'active',
    ];

    public function getProjectMetaAttribute()
    {
        return DB::table('projects_meta')
            ->where('project_id', $this->id)
            ->get();
    }
    public function getProjectDonationVariationAttribute()
    {
        return DB::table('projects_donation_varitation')
            ->where('project_id', $this->id)
            ->get();
    }
}

Please or to participate in this conversation.