我想在Laravel 6中向用户显示其姓名,日期,消息以及多个文件的消息。
Admin 2nd Oct 2019
this is my message.
file1, file2 ,file3,....
这就是我要显示数据的方式
这是我的控制器代码:
$data2=DB::table('order_deliver')
-> select('order_deliver.id' , 'order_deliver.deliver_by_id' , 'order_deliver.message' , 'order_deliver.created_at','order_deliver_attechments.attechment','order_deliver_attechments.deliver_id')
-> join('order_deliver_attechments','order_deliver_attechments.deliver_id','=','order_deliver.id')
-> where(['order_deliver.order_id' => $order_id])
//->->distinct()
//->groupBy('order_deliver_attechments.deliver_id')
-> get()->toArray();
return view("Admin.view.AdminViewSingleOrder" , ['adminMessage'=>$data2]);
blade.php代码
@foreach($adminMessage as $info)
<div class="pdl-20">
<h4 class="text-capitalize "><i class="fa fa-user pdr-10 fs-19" aria-hidden="true"></i> <b>Admin</b><span class="fs-10 pdl-10">{{$info->created_at}} 16 nov, 2019</span></h4>
<p><i class="fa fa-comments-o pdr-10 fs-19" aria-hidden="true"></i>{{$info->message}}</p>
<p><i class="fa fa-paperclip pdr-10 fs-19" aria-hidden="true"></i> <span class="font-weight-normal text-capitalize ">
<a href="{{--URL::to('/Orders_attachments/'.$info->attechment)--}}" download>{{$info->attechment}}, </a></span><p>
</div>
@endforeach
我正在获取数据,但是唯一的问题是,由于存在多个文件,因此其他信息也会与不需要的文件一起重复。
这是我的输出结果
答案 0 :(得分:0)
您可以定义模型之间的关系,就像假设您拥有OrderDeliver
和OrderDeliverAttechment
模型一样。检查外键名称是否不同。
class OrderDeliver extends Model
{
/**
* Get the OrderDeliverAttechments for the OrderDeliver.
*/
public function attachments()
{
return $this->hasMany(OrderDeliverAttechment::class);
}
}
现在,您可以像下面这样用雄辩的方式运行查询
$orderDelivers = OrderDeliver::where('order_id',$order_id)->with('attachments')->get();
现在,您将获得一个多维数组作为结果集合,并且您可以运行2个嵌套的foreach循环并正确显示数据
@foreach($orderDelivers as $orderDeliver)
//Display data from main table
@foreach($orderDeliver->attachments as $attachments)
//Display fields from order_deliver_attachment table
@endforeach
@endforeach