Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

coder72's avatar

How to export single data in laravel ?

Hello, I am trying export single product data, So I am using this command

composer require maatwebsite/excel

This is my ProductExport.php.Do i need to make another function for single detail ?

  public function collection() 
    {
        return Product::all();
    }

This is my route

  Route::get('export/{product}', 'ProductController@export')->name('prodcutExport');

In ProductController.php

 public function export()
    {
        return Excel::download(new ProductExport(), 'patient.xlsx');
    }

In product (index.blade.php)

@foreach($products as $product)
	<tr>{{$product->name}}</tr>
        <tr>{{$product->price}}</tr> 
        <tr><a href="{{route('prodcutExport',$product->id)}}">Export Product Detail</></tr>
@endforeach
0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122
    public function export()
    {
	
	$products = Product::where('id',1)->get();    // your rules... get the products you want to export as a collection

	// pass the products to the export class
        return Excel::download(new ProductExport($products), 'patient.xlsx');
    }

product export class


    public function __construct($products)
    {
        $this->products = $products;
     }


    public function collection() 
    {
        return $this->products;
    }

Please or to participate in this conversation.