Laravel如何从数据库下载txt文件

时间:2019-02-12 08:52:56

标签: laravel

我正在研究laravel mini项目,我有几个小问题,但是这个问题很棘手,我检查了所有可能的解决方案,没有任何效果。 当我单击下载时,下载过程开始,但是随后我在失败-服务器问题中收到消息。

enter image description here

我想下载以name-last_name.txt格式命名的文件,以防有两个人具有相同的名字name-last_name / 2.txt

如果您可以与我分享任何知识是正确的过程,我将不胜感激

控制器

<?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);


}
}

刀片文件为

     @extends('layout')
     @section('content')
     <div class="row">
         <div class="col-9 mt-5">
            <h1>Seznam klientů</h1>
        </div>
        <div class="col-3 mt-5">
            <h6><a href="/create">Nový klient</a></h6>
        </div>
     </div>
     <div class="container">
        @if(count($clients)>0)
            <ul class="list-group">
                <table class="table">
                        @foreach($clients as $client)

                        <table class="table">
                                <thead>

                                  <tr>
                                    <th scope="col">#</th>
                                    <th scope="col">Jméno</th>
                                    <th scope="col">Příjmení</th>
                                    <th scope="col">Telefonní číslo</th>
                                    <th scope="col">Provolaný čas</th>
                                    <th scope="col">Souhlas</th>
                                    <th scope="col">Stahnout data</th>

                                  </tr>
                                </thead>
                                <tbody>
                                  <tr>

                                    <th scope="row"> </th>
                                    <td>{{$client->name}}</td>
                                    <td>{{$client->last_name}}</td>
                                    <td>{{$client->phone}}</td>
                                    <td>{{$client->call_time}}</td>
                                    <td>{{$client->agree}}</td>
                                    <td><a href="download/{{$client->name}}" 
        download="{{$client->name}}">
                                        <button type="button" class="btn btn- 
                                               primary">
                                            Download
                                        </button>
                                    </a></td>
                                  </tr>

                                </tbody>
                              </table>

                @endforeach
            </ul>
        @else
            <p>Seznam je prázdny</p>
        @endif
    </div>

   @endsection

路线

Route::resource('/', 'FormsController');
Route::get('/{$id}', 'FormsController@download');

0 个答案:

没有答案