Search code examples
octobercmsoctobercms-pluginsoctobercms-backend

OctoberCMS: set multiple models in one API endpoint


I build backend with OctobderCMS for mobile app and need to have API for the app, i have around 15 models and want to have 2 API endpoint for these models, One endpoint for 2-3 models and the other enpoint for the rest of the models.

Can i make this happen?


Solution

  • Yes definitely, you can make APIs using all models of your OctoberCMS.

    You have to make routes.php file in your Plugin directory where your all models belong. And in routes.php file you have to maintain your results with queries.

    Example of routes.php file is below:

    <?php
    
    //Here you have to define your route.
    Route::get('api/v1/your_route', function () {
        $result = [];    //define a variable as array
    
        $wartaProfiles= \WartaProfil::where('id','1')->get();;
        $jadwalKeb = \JadwalKeb::all();
    
        $result['wartaProfiles'] = $wartaProfiles;
        $result['jadwalKeb'] = $jadwalKeb;
        return $result;
    });
    

    I have two models Recipe and Category so I have mixed both models results in $result and returning it. Now you can define your models and do whatever results you want.

    For more information about retrieving results from models, visit this Retrieving multiple models.

    That's all form my side. Hope this will help you.

    Comment if you have any doubts.