简体   繁体   中英

Laravel - Show information from 2 tables

I create a customer with the CRUD from Laravel. I add some base data like "Name, Birthday, Sex etc.).

Now I use the "show" route from Laravel and see the data that I saved in the database table (kundens).

From this view, I generate a PDF with a button. All this works for me. All the data come from my table "kundens". The view data come from this table, too. But how can I show data from more than one table in the same "view"? I need to add some more data in the PDF - not only from the "kundens" table.

My code for the customer view is this:

<div class="row">
            <div class="col-md-12 col-md-offset-12">
                <h4>Übersicht</h4>
                <h5>Persönliche Daten</h5>
                {{ $kunden->vorname }}<br>
                {{ $kunden->nachname }}<br>
                {{ $kunden->strasse }}<br>
                {{ $kunden->plz }}
                {{ $kunden->wohnort }}<br>

                <button class="btn btn-danger" style="color: #fff"><a style="color: #fff" href="{{asset('admin/generate_offer')}}{{ '/'.$kunden->id }}">Angebot erstellen</a></button>
                 <ul>
                @foreach($kunden['offer'] as $offer)
                    <li><a href="{{asset('admin/download_pdf')}}{{ '/'.$offer->id }}" download="">{{$offer->id}}</a> </li>
                @endforeach
            </ul>
            </div>
        </div>

There you see that I take the information from $kunden - but I need to take more information that saved in another table.

Does anyone know a solution?

Apparently, you are generating a PDF file in the admin/generate_offer related controller .

So in order to have more data, just get your data in that controller (that is responsible for generating PDF file) and simply add all of them no matter what and how many tables you want to get.

So first of all, you have to write your model and controller so we can see it better than what you write but here, for example, you want to make some other model, to show up here. So here is how you do it:

In your main model, kundens model you write like this:

public function somedata() {
    return $this->hasOne('App\YOURAPP','id','Table_id'); // in case you have the name of tables as ID not the Table_id you must not the second and third argument here 
}

and in your controller, you must write like this:

$kundens= Kundens::with('somedata')->get(); // here some data is the name of the function that u inserted in your controller

So you have to customize this a bit into your needs.

I hope this helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM