simber's avatar

Nova resource from API data

Is there a way to create a Nova resource that doesn't fetch from DB but instead from a remote API?

0 likes
13 replies
ekpono's avatar

I have this same question too. Please were you able to resolve it? @simber

simber's avatar

No, @ekpono , sadly I wasn't. It seems to me it is not possible.

ekpono's avatar

This is sad. Seems I will have to use custom admin panel

ekpono's avatar

I have successfully implemented this. I created a new connection and pointed the model to that connection, which is another database.

return array(

    'default' => 'mysql',

    'connections' => array(

        # Primary/Default database connection
        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => '127.0.0.1',
            'database'  => 'database1',
            'username'  => 'root',
            'password'  => 'secret'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

        # Secondary database connection
        'mysql2' => array(
            'driver'    => 'mysql',
            'host'      => '127.0.0.1',
            'database'  => 'database2',
            'username'  => 'root',
            'password'  => 'secret'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    ),
);

On the model, I used

class SomeModel extends Eloquent {

    protected $connection = 'mysql2';

}
1 like
zhxscript's avatar
Level 2

Yes, it possible to do this. I am in the midst of doing this now.

in short, my approach was:

1.) Code Services to interact with my API 2.) Code Custom Rules and use model Get Attributes 3.) Use Event Listeners that will call my API service and run the required tasks 4.) Do whatever/Override any data required locally 5.) Local Data will update/save/etc.

In my current instance, I have setup a micro-service to handle all aspects of my users and their profiles. The service will handle everything from validation of user profile data to authenticating a session, all through API. When I create a user, Instead of saving to the DB, I save to the API and then simply store the UID locally. I have leveraged, setting attributes, custom validation rules, to call API endpoints and customizing the resources functions to all fit my need. As of this post everything is working fantastically and only thing left is to work in being able to use the Searching functions for Nova to be able to find the resources as if thee data was local.

This is my first time every playing with Nova, in fact I only just bought the license last night. Being able to accomplish this is really really exciting and looking forward to being able to mast nova like I have with Laravel.

Feel free to contact me if you have any other questions.

4 likes
Tektōn's avatar

@zhxscript I want to know more the coding of how to do this. I'd like to do PUT calls with my Customer resource details to an API endpoint

zhxscript's avatar

Just Updating.I am successfully able to maintain searching ability across my API based "models"

simber's avatar

Thanks for the tip. Do you have an example that you could share? I currently just fetch the data from the API and save it to a table but it's a temporary solution as I needed a solution promptly.

Best regards, Simon

packy's avatar

@simber @zhxscript Was there any examples anyone can post? I was thinking of adding a "Products" resource that connected to a Shopify account.

shllghst's avatar

Hey @zhxscript can you provide a small example of how to implement it please ? I really am intrested too. Thanks in advance.

Priit's avatar

I have not tried it myself but calebporzio/sushi Eloquent array driver might be the missing link to present arrays for Nova as if they come from db.

feeh27's avatar

You can write a custom Laravel Scout Engine to interact with your API (search only) and use Laravel events to write operations (create, update and delete).

Please or to participate in this conversation.