lundi 30 juillet 2018

how to define a named route and download a file from database in laravel

I have my url defined in blade as:

href="/finance/invoices/download/"

and the route in web.php is:

Route::get('invoices/download/{year}/{month}/{file}', 'InvoiceController@download');

my file is stored in the database as:

2018/07/invoiceberry_invoice_template_1.docx

edit function in my controller is:

public function edit(Invoice $invoice)
{
    $projectStageBillings = $invoice->projectStageBillings;

    $projectStageBilling = $projectStageBillings->first();
    $client = $projectStageBilling->projectStage->project->client;
    $client->load('projects', 'projects.stages', 'projects.stages.billings');

    $billings = [];
    foreach ($projectStageBillings as $key => $billing) {
        $billing->load('projectStage', 'projectStage.project');
        $billings[] = $billing;
    }

    return view('finance.invoice.edit')->with([
        'invoice' => $invoice,
        'clients' => Client::select('id', 'name')->get(),
        'invoice_client' => $client,
        'invoice_billings' => $billings,

    ]);
}

and download method is :

public function download($year, $month, $file, $inline = true)
{
    $headers = [
        'content-type' => 'application/pdf',
    ];

    $file_path = FileHelper::getFilePath($year, $month, $file);

    if (!$file_path) {
        return false;
    }

    if ($inline) {
        return Response::make(Storage::get($file_path), 200, $headers);
    }

    return Storage::download($file_path);
}

how do I do that?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2vog5oT
via IFTTT

Aucun commentaire:

Enregistrer un commentaire