How to export product csv with full url of product

Stefan Bogdanescu
Founder & Senior Architect · 23 July 2026

Exporting Product CSV with Full URLs in Laravel

As a senior developer, I often encounter this question from e-commerce business owners: “How can I export my product CSV with full URLs including the base URL automatically? It’s too tedious to do it manually.”

The good news is, you can definitely automate this process using Laravel’s built-in export functionality. Here’s how:

Understanding the Problem

By default, when you export a product CSV in Laravel, the url field only contains the relative path of the product (e.g., /product/123). However, you need the full URL including your domain name.

To solve this, we’ll need to customize the export process to generate the URLs on-the-fly using PHP’s built-in functions. Let me show you how:

Step-by-Step Solution

1. Create a new command

First, let’s create a custom Artisan command for product exports. Open your terminal and run this command:

php artisan make:command ExportProducts

This will create a new ExportProducts class in the app/Console/Commands directory.

2. Modify the handle method

Open the newly created ExportProducts.php file and modify the handle() method as follows:

use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;

public function handle()
{
    $products = App\Models\Product::all();

    $csv = fopen('php://temp', 'w');
    fputcsv($csv, ['ID', 'Name', 'Price', 'URL']);

    foreach ($products as $product) {
        fputcsv(
            $csv,
            [
                $product->id,
                $product->name,
                number_format($product->price),
                url('product/' . $product->id)
            ]
        );
    }

    Storage::disk('local')->put('exports/products-' . Carbon::now()->format('Y-m-d') . '.csv', stream_get_contents($csv));
    fclose($csv);
}

What this code does:

  • It retrieves all products from the database.
  • It opens a temporary CSV file in write mode.
  • It writes headers for the CSV: ID, Name, Price, and URL.
  • For each product, it generates a full URL using Laravel’s url() helper function, which includes your domain name.
  • It writes this data to the CSV row by row.
  • Finally, it saves the temporary file in the local storage folder with today’s date as the filename.

3. Run the command

Now you can run your new command from the terminal:

php artisan export:products

The script will generate a new CSV file called exports/products-YYYYMMDD.csv containing all products with full URLs in your project’s root directory.

Best Practices and Gotchas

  • Security: Ensure your exported files are stored securely, especially if they contain sensitive data. Consider using a private storage disk or restricted access to the export folder.

  • Performance: If you have thousands of products, consider using Laravel’s chunk() method inside the loop to process only small batches at a time and free up memory between chunks.

Conclusion

Automating product CSV exports with full URLs is not only possible but also quite straightforward in Laravel. By creating a custom command and generating the URLs on-the-fly, you can save countless hours of manual work every month.

Remember, automation is key to scaling your e-commerce business. Whenever a repetitive task can be automated, take the time to write the code—it pays off in the long run by reducing errors and freeing up your team’s time for more strategic tasks.

If you’re looking for more advanced export solutions like scheduling automatic exports or integrating with external systems, check out our guide on Laravel CSV Export Best Practices and our premium Laravel packages at the Laravel Company store.

Our network