我有两张表,一张是关于订单的一般信息orders
,一张detail
是产品、备注和交货日期等信息。
基本上,我想在同一页面中显示表格订单,并且每个订单可以是多个产品、注释、表格详细信息中的日期,而且我想分页以便在每个页面和每个产品上准确显示 20 个订单,请注意,具有相同 id_order 的日期行。
总之,在每一页上有来自表格订单的 20 个订单,但表格详细信息可以有 40 行,包括产品、备注、交货日期,因为一个订单可以包含多个产品。事实上,我想使用 rowspan html 以便在同一行上显示详细信息。
这是我的订单模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class order extends Model
{
public $timestamps = false;
protected $fillable = ['client','number_order','file'];
public function details()
{
return $this->hasMany('App\Detail','orders_id')->orderBy('id', 'desc');
}
}
详细型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Detail extends Model
{
protected $table = 'detail';
public function order()
{
return $this->belongsTo('Detail','orders_id');
}
}
我的控制器:
public function index()
{
$orders = Order::first(); //This works only with one record I cant show all records, all() is not working
$orders->setRelation('details', $orders->details()->paginate(10));
return view('orders.index', compact('orders'));
}
查看:
<tbody>
@foreach ($orders->details as $detail)
<tr id="{{ $detail->orders_id }}">
<td>{{ $detail->product }}</td>
</tr>
@endforeach
</tbody>
您可以像这样在订单上使用预先加载详细信息
public function index()
{
$orders = Order::with('details')->paginate(20);
return view('orders.index', compact('orders'));
}
在视图中
<table>
<tbody>
@foreach($orders as $order)
<tr>
<td>{{ $order->created_at }}</td>
<td>... put any order info here</td>
<td>
<label>Order Details:</label>
<table>
@foreach ($order->details as $detail)
<tr id="{{ $detail->orders_id }}">
<td>{{ $detail->product }}</td>
<td>{{ $detail->note }}</td>
</tr>
@endforeach
</table>
<td>
</tr>
@endforeach
</tbody>
</table>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句