Search code examples
laravelcommandeloquentlaravel-artisan

Laravel 5.1 Command empty table


I'm using a command to add products from an API to my database using the following code

class UpdateCatalog extends Command {

    protected $name = 'catalog:update';
    protected $description = 'Command description.';

    public function __construct()
    {
        parent::__construct();
    }

    public function fire()
    {

        $products = Api::productsGetProducts();

        foreach($products as $product)
        {
            $detail = Api::productsGetProduct($product['id']);
            $product = new Product();
            $product->id = $detail->getId();
            $product->external_id = $detail->getExternalId();
            $product->name = $detail->getName();
            $product->description = $detail->getDescription();
            $product->thumbnail = $detail->getThumbnail();
            $product->price = $detail->getPrices()[0]['price_excl_vat'];
            $product->vat = $detail->getVat();
            $product->save();
        }
    }
}

Now I'm wondering if it's possible to empty the table prior to filling it again.

Thank you!


Solution

  • Do you mean you want to empty your Product database table?

    This can be done with truncate like this:

    Product::truncate();

    Note: This will remove all rows and reset the auto-incrementing ID to zero