Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a PDF to customer - laravel 5.7

I have a small CRM system. I can create, update and delete a customer. I have also a detailed view for every customer. Now I want at the end of the single view a button: Create Offer.

I have 2 Tables. 1 Table have the name: customer with some fields Name, Surname etc. I create the customer like this:

<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>

The detailed view is similar to that page. In the detailed view I made a button. The button is linked to the dynamic PDF Controller. The Dynamic PDF controller worked but I dont know how I get the data from the detailed view customer. I only get the data from ALL customers in the table.

here is the snippet:

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

I understand that this is wrong (iam beginner sorry for that) But I dont know how I can code, that I get the data from the customer that I chossed in the detailed view.

When I clicked on the button I want that the PDF is saved in the Database and is linked to that customer.

I hope I explained cleraly. When not please dont downrate - I try to explain better when its not enough.

like image 852
Dierig Patrick Avatar asked Nov 08 '22 01:11

Dierig Patrick


1 Answers

If I Understand correctly: You only want to get the data from one specific customer on button click?

Button link with kunden id:

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

New Route:

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

Controller update:

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;
}

---- EDIT ----

A Complete guide:

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:

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

Controller:

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;
    }
}
like image 183
Patrick Avatar answered Nov 14 '22 21:11

Patrick