我使用 Laravel 的 CRUD 创建了一个客户。我添加了一些基本数据,例如“姓名、生日、性别等)。
现在我使用 Laravel 中的“show”路由并查看我保存在数据库表 (kundens) 中的数据。
从此 View 中,我生成了带有按钮的 PDF。这一切都对我有用。所有数据都来 self 的表“kundens”。 View 数据也来自该表。但是如何在同一个“ View ”中显示多个表中的数据呢?我需要在 PDF 中添加更多数据 - 不仅仅是“kundens”表中的数据。
我的客户 View 代码是这样的:
<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>
您可以看到我从 $kunden 获取了信息 - 但我需要获取保存在另一个表中的更多信息。
有人知道解决办法吗?
最佳答案
因此,首先,您必须编写模型和 Controller ,以便我们可以比您编写的内容更好地看到它,但是在这里,例如,您想要制作一些其他模型,以显示在此处。因此,您可以这样做:
在您的主模型、kundens 模型中,您可以这样编写:
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
}
在你的 Controller 中,你必须这样写:
$kundens= Kundens::with('somedata')->get(); // here some data is the name of the function that u inserted in your controller
因此,您必须根据您的需求对此进行一些自定义。
我希望这会有所帮助...
关于php - Laravel - 显示 2 个表中的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53186773/