Back

Download txt.file Laravel 5.4


I am working on laravel mini project, I have couple small issues, but this one is pretty tricky, I checked all possible solution, nothing worked. When I click on download, downloading process starts, but then I got message in Failed - No File

I would like download file named in format name-last_name.txt, incase there are two people with the same name name-last_name/2.txt I stored data in database, when I clicked on button I would like txt file with one client information.

If you can share with me any knowledge is the correct process of it, I will appreciate a lot

Controller

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Storage;
use App\Client;

class FormsController extends Controller
{
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    //
    $clients = Client::all();
    return view('/index')->with('clients', $clients);
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    //
    return view ('create');

}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    //
    $this->validate($request, [
        'name' => 'required',
        'last_name' => 'required',
        'phone'=> 'required',
        'agree'=> 'required'
  ]);

    $client = new Client;
    $client->name = $request->input('name');
    $client->last_name = $request->input('last_name');
    $client->phone = $request->input('phone');
    $client->call_time = $request->input('call_time');
    $client->agree = $request->input('agree');
    $client->save();
    return redirect('/')->with('success', 'Klient vytvořen');
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    //
    $client = Client::find($id);
    return view ('show')->with('client', $client);
}

public function download(){

    $client = Client::find($id);
   return response()->download($pathToFile, $name, $last_name);


}
}

Blade file

@extends('layout')

@section('content')
    <div class="row">
        <div class="col-9 mt-5 text-center">
            <h1>Seznam klientů</h1>
        </div>
        <div class="col-3 mt-5 mb-5">
            <h6><a href="/create">Nový klient</a></h6>
        </div>
    </div>
    <div class="container">
        @if(count($clients)>0)
        <div class="row font-weight-bold ">
            <div class="col">
                    Jméno
            </div>
            <div class="col">
                    Příjmení
            </div>
            <div class="col">
                    Telefonní číslo
            </div>
            <div class="col">
                    Provolaný čas
            </div>
            <div class="col">
                    Souhlas
            </div>
            <div class="col">
                    Stahnout data
            </div>
        </div>
        @foreach($clients as $client)
        <div class="row mt-1">
                <div class="col">
                    {{$client->name}}
                </div>
                <div class="col">
                    {{$client->last_name}}
                </div>
                <div class="col">
                    {{$client->phone}}
                </div>
                <div class="col">
                    {{$client->call_time}}
                </div>
                <div class="col">
                    {{$client->agree}}
                </div>
                <div class="col">
                    <a href="download/{{$client->name}}" download="{{$client->name}}"><button type="button" class="btn btn-primary">Download</button></a>
                </div>
            </div>
            @endforeach
        @else
            <p>Seznam je prázdny</p>
        @endif
    </div>

@endsection

Route

Route::resource('/', 'FormsController');
Route::get('/{$id}', '[email protected]');
Cameron replied 2 months ago

{{$client->name}}-{{$client->last_name}}.txt do you have a file called that? or are you trying to create a txt file from the data?


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us