This Laravel project includes custom Artisan commands, XmlStructure, and InsertProductData, designed to inspect an XML file, transform its structure, and store the data in a database with CLI. This can be particularly useful for handling XML data seamlessly in a Laravel application.
Clone the repository from GitHub:
git clone git@github.com:shoaib0300/write-data-to-database-with-CLI.gitNavigate to the project directory:
cd write-data-to-database-with-CLIRun Composer to install project dependencies:
composer installEnsure your Laravel environment is properly configured, and the database connection details are set in the .env file.
DB_CONNECTION=mysql # Database connection type (e.g., mysql)
DB_HOST=localhost # Database host (e.g., localhost)
DB_PORT=3306 # Database port (usually 3306 for MySQL)
DB_DATABASE=laravel_project # Database name
DB_USERNAME=root # Database username
DB_PASSWORD= # Database passwordRun database migrations:
php artisan migrateBefore using the XmlStructure and InsertProductData commands, make sure to follow these steps:
Upload the XML file you want to process into the storage/app/uploads/ directory.
No additional installation steps are required. The command is ready to use once you've uploaded your XML file.
Execute the following command in your terminal to see the uploaded file content in JSON format:
php artisan xml:inspect {filename}
# Example:
php artisan xml:inspect feed.xmlTo insert product data into the database from the XML file, you can use the product:insert Artisan command. Follow these steps:
-
Ensure that you have a valid
feed.xmlfile in thestorage/app/uploads/directory. -
Open a terminal or command prompt and navigate to your Laravel project's root directory.
-
Run the following command to insert product data into the database:
php artisan product:insert
-
The command will process the XML file, check for duplicates, and insert non-duplicate product data into the database.
-
If any errors occur during the process, they will be logged in
/storage/logs/custom.log, and you'll be notified.
Note: Make sure your database connection settings in the .env file are correctly configured.
To run PHPUnit test cases for XmlStructureTest, use the following command:
php artisan test --filter XmlStructureTestTo run PHPUnit test cases for InsertProductDataTest, use the following command:
php artisan test --filter InsertProductDataTestTo clear the configuration cache, use:
php artisan config:clear