添加一个pdf到customer-laravel 5.7

pgky5nke  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(373)

我有一个小型客户关系管理系统。我可以创建、更新和删除客户。我也为每个客户提供了详细的视图。现在我想在单个视图的末尾添加一个按钮: Create Offer .
我有两张table。1个表的名称为:customer,其中包含一些字段name、姓氏等。我这样创建customer:

<form method="post" action="/mvs/mvs/public/admin/kunden">
    {{ csrf_field() }}
    <div class="container">
        <div class="row">
            <div class="col-md-12 col-md-offset-2" >

                <div class="form-group">
                    <label for="vorname">Vorname</label>
                    <input type="text" class="form-control" name="vorname" id="vorname" placeholder="Vorname" value="{{ old('vorname') }}" required>
                </div>
                <div class="form-group">
                    <label for="nachname">Nachname</label>
                    <input type="text" class="form-control" name="nachname" id="nachname" placeholder="Nachname" value="{{ old('nachname') }}" required>
                </div>

                <div class="form-group">
                    <label for="strasse">Straße</label>
                    <input type="text" class="form-control" name="strasse" id="strasse" placeholder="Strasse" value="{{ old('strasse') }}" required>
                </div>

                <div class="form-group">
                    <label for="plz">PLZ</label>
                    <input type="number" class="form-control" name="plz" id="plz" placeholder="Plz" value="{{ old('plz') }}" required>
                </div>

                <div class="form-group">
                    <label for="wohnort">Wohnort</label>
                    <input type="text" class="form-control" name="wohnort" id="wohnort" placeholder="Wohnort" value="{{ old('wohnort') }}" required>
                </div>

                 <div class="form-group">
                    <label for="mail">Mail</label>
                    <input type="mail" class="form-control" name="mail" id="mail" placeholder="E-mail" value="{{ old('mail') }}" required>
                </div>

                <div class="form-group">
                    <label for="telefon">Telefon</label>
                    <input type="text" class="form-control" name="telefon" id="telefon" placeholder="Telefon" value="{{ old('telefon') }}" required>
                </div>

                <div class="form-group">
                    <label for="geburtsdatum">Geburtsdatum</label>
                    <input type="date" class="form-control" name="geburtsdatum" id="geburtsdatum" placeholder="Geburtsdatum" value="{{ old('geburtsdatum') }}" required>
                </div>

                <br>

                <button type="submit" class="btn btn-primary">Kunden anlegen</button>
                <a href="{{ URL::previous() }}"><button type="submit" class="btn btn-danger">Abbrechen</button></a>

            </div>
        </div>
    </div>
</form>

详细视图与该页类似。在详细视图中,我做了一个按钮。该按钮链接到动态pdf控制器。动态pdf控制器工作,但我不知道我如何从客户的详细视图数据。我只得到表中所有客户的数据。
以下是片段:

function get_customer_data()
{
 $customer_data = DB::table('kundens')
     ->limit(10)
     ->get();
 return $customer_data;
}

我明白这是错误的(我是初学者,对此很抱歉),但我不知道如何编码,我从客户那里得到了我在详细视图中选择的数据。
当我点击按钮时,我希望pdf保存在数据库中并链接到该客户。
我希望我能解释清楚。没有的时候请不要轻描淡写-我尽量解释得更好,但这还不够。

s2j5cfk0

s2j5cfk01#

在详细视图页面中,按钮(链接)如下所示:

<a href="/public/pdf/{$customer->id}"><button type="submit" class="btn btn-danger">Abbrechen</button></a>

您的路线(web.php):

Route::get('/pdf/{id}', DynamicPDFController@index');

您的dynamicpdfcontroller将是:

function get_customer_data($id){
    $customer_data = DB::table('kundens')
             ->find($id);

//Paste the PDF converting code here to convert the data on $customer collection.
        return $customer_data;
        }
mccptt67

mccptt672#

如果我理解正确:你只想从一个特定的客户的数据按钮点击?
带有昆登id的按钮链接:

<a href="/mvs/mvs/public/admin/kunden/pdf/{{ $kunden->id }}">Button html</a>

新路线:

Route::get('/mvs/mvs/public/admin/kunden/pdf/{id}', ControllerName@get_customer_data');

控制器更新:

function get_customer_data($id)
{
 //Handle PDF stuff here 

 $customer_data = DB::table('kundens')
     ->where('id', '=', $id)
     ->firstOrFail();

//Save PDF link to customer here

 $customer_data->save();
 return $customer_data;
}

----编辑----
完整指南:
html格式:

<form method="post" action="/mvs/mvs/public/admin/kunden/pdf/{{ $kunden->id }}">
    {{ csrf_field() }}
    <div class="container">
        <div class="row">
            <div class="col-md-12 col-md-offset-2" >

                <div class="form-group">
                    <label for="vorname">Vorname</label>
                    <input type="text" class="form-control" name="vorname" id="vorname" placeholder="Vorname" value="{{ old('vorname') }}" required>
                </div>
                <div class="form-group">
                    <label for="nachname">Nachname</label>
                    <input type="text" class="form-control" name="nachname" id="nachname" placeholder="Nachname" value="{{ old('nachname') }}" required>
                </div>

                <div class="form-group">
                    <label for="strasse">Straße</label>
                    <input type="text" class="form-control" name="strasse" id="strasse" placeholder="Strasse" value="{{ old('strasse') }}" required>
                </div>

                <div class="form-group">
                    <label for="plz">PLZ</label>
                    <input type="number" class="form-control" name="plz" id="plz" placeholder="Plz" value="{{ old('plz') }}" required>
                </div>

                <div class="form-group">
                    <label for="wohnort">Wohnort</label>
                    <input type="text" class="form-control" name="wohnort" id="wohnort" placeholder="Wohnort" value="{{ old('wohnort') }}" required>
                </div>

                 <div class="form-group">
                    <label for="mail">Mail</label>
                    <input type="mail" class="form-control" name="mail" id="mail" placeholder="E-mail" value="{{ old('mail') }}" required>
                </div>

                <div class="form-group">
                    <label for="telefon">Telefon</label>
                    <input type="text" class="form-control" name="telefon" id="telefon" placeholder="Telefon" value="{{ old('telefon') }}" required>
                </div>

                <div class="form-group">
                    <label for="geburtsdatum">Geburtsdatum</label>
                    <input type="date" class="form-control" name="geburtsdatum" id="geburtsdatum" placeholder="Geburtsdatum" value="{{ old('geburtsdatum') }}" required>
                </div>

                <br>
<input type="hidden" name="kunden-id" value="{{ $kunden->id }}" />
                <button type="submit" class="btn btn-primary" >Kunden anlegen</button>

                <a href="{{ URL::previous() }}"><button type="submit" class="btn btn-danger">Abbrechen</button></a>

            </div>
        </div>
    </div>
</form>

路线:

Route::get('/mvs/mvs/public/admin/kunden/pdf/{id}', DynamicPDFController@index');

控制器:

namespace MVS\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use PDF;
use MVS\Kunden;

class DynamicPDFController extends Controller
{
    function index(Request $request)
    {

     $data = $request->all();

     $id = $data['kunden-id'];
     $customer_data = $this->get_customer_data($id);
     //$finance_data = $this->get_finance_data();
     return view('dynamic_pdf')->with('customer_data', $customer_data);
    }

    function get_customer_data($id)
    {
     $customer_data = Kunden::whereId($id)->first();
     return $customer_data;
    }

    function pdf()
    {
     $pdf = \App::make('dompdf.wrapper');
     $pdf->loadHTML($this->convert_customer_data_to_html());
     return $pdf->stream();
    }

    function convert_customer_data_to_html()
    {
     $customer_data = $this->get_customer_data();
     $output = '
     <h3 align="center">Angebot</h3>
     <table width="100%" style="border-collapse: collapse; border: 0px;">
      <tr>
    <th style="border: 1px solid; padding:12px;" width="20%">Vorname</th>
    <th style="border: 1px solid; padding:12px;" width="30%">Nachname</th>
    <th style="border: 1px solid; padding:12px;" width="15%">Stadt</th>
    <th style="border: 1px solid; padding:12px;" width="15%">PLZ</th>
   </tr>
     ';  
     foreach($customer_data as $kunden)
     {
      $output .= '
      <tr>
       <td style="border: 1px solid; padding:12px;">'.$kunden->vorname.'</td>
       <td style="border: 1px solid; padding:12px;">'.$kunden->nachname.'</td>
       <td style="border: 1px solid; padding:12px;">'.$kunden->wohnort.'</td>
       <td style="border: 1px solid; padding:12px;">'.$kunden->plz.'</td>
      </tr>
      ';
     }
     $output .= '</table>';
     return $output;
    }
}

相关问题