Php 关于如何从Laravel 5.5中的3个不同SQL表在视图中填充HTML表的建议?
希望你能帮我解决我的问题。我想做的是从3个不同SQL表中的查询数据填充视图中的表 我该如何填写: 根据收集的数据: MySQL查询:Php 关于如何从Laravel 5.5中的3个不同SQL表在视图中填充HTML表的建议?,php,mysql,laravel,laravel-5.5,Php,Mysql,Laravel,Laravel 5.5,希望你能帮我解决我的问题。我想做的是从3个不同SQL表中的查询数据填充视图中的表 我该如何填写: 根据收集的数据: MySQL查询: -- Ordered Query SELECT abstract_supplier.supplier,abstract_supplier.canvasser_name,abstract_supplier.canvasser_department, abstract_items.particulars, abstract_items.qty,abstract_
-- Ordered Query
SELECT abstract_supplier.supplier,abstract_supplier.canvasser_name,abstract_supplier.canvasser_department, abstract_items.particulars, abstract_items.qty,abstract_items.unit,abstract_price.unit_price, abstract_price.total_price
-> FROM abstract_items
-> RIGHT JOIN abstract_price ON abstract_price.item_id = abstract_items.id
-> LEFT JOIN abstract_supplier ON abstract_supplier.id = abstract_price.supplier_id;
我的控制器:
public function show($id)
{
$abstract = AbstractModel::find($id);
$office = Office::all();
$pr_item = PurchaseRequestItemModel::all()->where('pr_form_number',$abstract->pr_number);
$grand_total = $pr_item->sum('pr_estimated_cost');
$abstract_items = abstractitemmodel::all()->where('abstract_id',"=",$abstract->id);
$query= DB::table('abstract_supplier')
->paginate(3);
// dd($query);
return view('abstract.abstract-form',compact('abstract','abstract_items','pr_item','grand_total','office','query','supp_query'));
}
我的看法
<div>
<table class="table table-responsive table-bordered table-condensed">
<thead class="text-center">
<tr class="center-t">
<th rowspan="3" class="col-xs-3">Particulars</th>
<th rowspan="3" class="col-xs-1">Qty</th>
<th rowspan="3" class="col-xs-1">Unit</th>
@php $counter = 0; @endphp
@foreach($query as $key => $suppliername)
@php $counter++; @endphp
<th colspan="2" class="col-xs-2">Supplier {{$counter}}</th>
@endforeach
@if($query->count() < 3)
@for($i = $query->count(); $i < 3; $i++)
<th colspan="2" class="col-xs-2">Supplier</th>
@endfor
@endif
</tr>
<tr>
@foreach($query as $indexKey => $suppliers)
<td colspan="2" class="col-xs-2 someCell">{{$suppliers->supplier}}</td>
@endforeach
@if($query->count() < 3)
@for($i = $query->count(); $i < 3; $i++)
<td colspan="2" class="col-xs-2">N/A</td>
@endfor
@endif
</tr>
<tr class="center-t">
@foreach($query as $key => $prices)
<th class="col-xs-1">Price/Unit</th>
<th class="col-xs-1">Price/Item</th>
@endforeach
@if($query->count() < 3)
@for($i = $query->count(); $i < 3; $i++)
<th class="col-xs-1">Price/Unit</th>
<th class="col-xs-1">Price/Item</th>
@endfor
@endif
</tr>
</thead>
<tbody>
@foreach($abstract_items as $key2 => $items)
<tr>
<td>{{$items->particulars}}</td>
<td>{{$items->qty}}</td>
<td>{{$items->unit}}</td>
@foreach($query as $price_key => $prices)
<td class="text-right"></td>
<td class="text-right"></td>
@endforeach
@if($query->count() < 3)
@for($i = $query->count(); $i < 3; $i++)
<td class="col-xs-1">N/A</td>
<td class="col-xs-1">N/A</td>
@endfor
@endif
</tr>
@endforeach
</tbody>
</table>
{{$query->links()}}
</div>
详情
数量
单元
@php$counter=0@endphp
@foreach($key=>$suppliername的查询)
@php$counter++@endphp
供应商{{$counter}
@endforeach
@如果($query->count()<3)
@对于($i=$query->count();$i<3;$i++)
供应商
@结束
@恩迪夫
@foreach($indexKey=>$suppliers作为查询)
{{$suppliers->supplier}
@endforeach
@如果($query->count()<3)
@对于($i=$query->count();$i<3;$i++)
不适用
@结束
@恩迪夫
@foreach($key=>$prices的查询)
价格/单位
价格/项目
@endforeach
@如果($query->count()<3)
@对于($i=$query->count();$i<3;$i++)
价格/单位
价格/项目
@结束
@恩迪夫
@foreach($key2=>$items形式的抽象项)
{{$items->details}
{{$items->qty}
{{$items->unit}
@foreach($price\u key=>$prices查询)
@endforeach
@如果($query->count()<3)
@对于($i=$query->count();$i<3;$i++)
不适用
不适用
@结束
@恩迪夫
@endforeach
{{$query->links()}
如您所见,我使用foreach循环并填充表格,但结果并非我所期望的,我希望您能就如何处理此问题提出见解和建议。尽管我会重命名您的抽象模型,因为它会给其他程序员带来严重的混乱,并且不遵循惯例,我想我明白你在追求什么:
class AbstractModel extends Model
{
// protected $table = 'abstract';
protected $fillable = [
'id',
'created_at',
'created_by' ,
'abstract_no',
'pr_number',
'proc_details',
'office',
'requestor_name'
];
public function offices()
{
return $this->hasOne('App\Office', 'id', 'office');
}
public function purchaseRequestItemModels()
{
return $this->hasOne('App\PurchaseRequestItemModel', 'id', 'pr_number');
}
}
Class Office extends Model
{
...
public function abstractModels()
{
return $this->belongsTo('App\AbstractModel', 'office', 'id');
}
}
class PurchaseRequestItemModel extends Model
{
...
public function abstractModels()
{
return $this->belongsTo('App\AbstractModel', 'pr_number', 'id');
}
}
然后,您可以继续实例化AbstractModel并捕获链接的办公室和PRNumber,如下所示:
$abstract = AbstractModel::find($id);
//These method calls return Objects of the type:
// Office
$abstract->offices();
//PurchaseRequestItemModel
$abstract->purchaseRequestItemModels();
现在,请检查以进一步改进您的语义和代码质量,因为这将在将来有很大帮助
注意:我知道您可能会有其他的类
彼此对话,如果您像我上面举例的那样建立关系,您可以获得相同的结果
有关进一步的关系解释,请参见Oi,这还有一些工作要做。我现在没时间了。如果明天还没有答复,我将提供帮助。同时,我建议研究雄辩的关系,然后将数据传递给水合器,然后是编组器,这样您的视图只会担心显示问题。@DiogoSanto为真。我不熟悉水合器和马歇尔勒这两个术语。这些是什么?所以它们是一些框架所采用的模式(非强制性)。我看过它们,发现它们非常适合作为应用代码时的默认规则使用。例如,一个程序员将负责用特定视图所需的内容和变量填充对象/数组。同时,封送员负责以我们希望呈现给视图的方式解析数据。你可以阅读更多关于封送员的内容。这并不意味着你会同时使用这两种方法,而是取决于你如何规划你的程序结构。例如,有时数据很简单,无法证明水合器的合理性。但我最近了解到,通常我们可以更普遍地使用封送拆收器:)我对检查您的代码有一个问题。你的
AbstractModel
应该是什么?谢谢你的回答,我在数据库关系方面没有那么多地使用雄辩模型的原因是,我不太理解它在雄辩中是如何工作的。希望我能提高我的代码质量,我仍在学习正确的方法欢迎您!编程是一门不断学习的课程。你正在走一条我们大家都共享的道路^^祝你进步好运!