vanilophp / cart

Cart Module for Vanilo (or any Laravel app)
https://vanilo.io
MIT License
50 stars 17 forks source link

Configurable Table Name for Cart Model #18

Closed adrienne closed 2 years ago

adrienne commented 2 years ago

Currently Vanilo assumes that the table name is carts, and there is no way to configure this. Can you make this a config setting, please?

I'll try to submit a pull request.

fulopattila122 commented 2 years ago

Hi Adrienne,

we have no plans to make the table names configurable, but you can easily achieve it in your project with <30 minutes of work.

Here's what to do:

  1. Disable the built-in migrations of the Cart module: config/concord.php:
    'modules' => [
        Vanilo\Cart\Providers\ModuleServiceProvider::class => [
            'migrations' => false
        ]
    ]
  2. Publish the migrations:
    php artisan vendor:publish --provider="Vanilo\Cart\Providers\ModuleServiceProvider" --tag="migrations"

    This will copy the Cart Module's migrations into your app's database/ folder.

  3. Modify the table names in those migrations. Now you can run php artisan migrate.
  4. Create Cart.php and CartItem.php files in the app/Models/ folder.
  5. Add the following content to Cart.php:
    class Cart extends Vanilo\Cart\Models\Cart
    {
       protected $table = 'your_cart_table_name';
    }
  6. Add the following content to CartItem.php:
    class CartItem extends Vanilo\Cart\Models\CartItem
    {
       protected $table = 'your_cart_items_table_name';
    }
  7. Add these lines to app/Providers/AppServiceProvider.php:

    namespace App\Providers;
    
    use Illuminate\Support\ServiceProvider;
    use Vanilo\Cart\Contracts\Cart as CartContract;
    use Vanilo\Cart\Contracts\CartItem as CartItemContract;
    
    class AppServiceProvider extends ServiceProvider
    {
       public function boot()
       {
           $this->app->concord->registerModel(CartContract::class, \App\Models\Cart::class);
           $this->app->concord->registerModel(CartItemContract::class, \App\Models\CartItem::class);
       }
    }

Now you can use the cart module with your custom table names.

Notes

References & Further Info

adrienne commented 2 years ago

@fulopattila122 - this did work, thank you!