php - 如何处理刀片 laravel
问题描述
我的控制器功能是。我通过投标人数组传递数据,但数组数据无法打印
public function bidders()
{
$payments = \App\Models\Payment::all();
$posts = Post::all()->where('user_id','=',Auth::user()->id);
foreach ($posts as $post) {
$postid = $post->id;
$bidder[] = Bid::all()->where('post_id','=',$postid);
}
return view('user.student.student-post-bidders',compact('bidder','posts','payments'));
}
这是我在这个刀片中的刀片,我从 DB 打印的数据。如何打印数组?刀片中的阵列打印索引
@php
dd($bidder);
$index = 0;
@endphp
@foreach($bidder as $bid)
@foreach($posts as $post)
@if($post->id == $bid[$index]['post_id'])
<div class="row" >
<!-- Dashboard Box -->
<div class="col-xl-12" >
<div class="dashboard-box margin-top-0">
<!-- Headline -->
<div class="headline">
<h4><strong><span class="bg-danger text-center" style="margin-left: 46px">Your Post Title:</span>{{$bid[$index]->Post->title}}</strong></h4>
</div>
<div class="content">
<ul class="dashboard-box-list">
<li>
<!-- Overview -->
<div class="freelancer-overview manage-candidates">
<div class="freelancer-overview-inner">
<!-- Avatar -->
<div class="freelancer-avatar">
<div class="verified-badge"></div>
@if($bid[$index]->Tutor->profile_Image=='')
<img data-cfsrc="{{asset('asset/images/user-avatar-placeholder.png')}}" alt="" style="display:none;visibility:hidden;">
@else
{{-- {{asset('storage/app/public/profileImages/' . $pic->profile_Image)}}--}}
<a href=""><img data-cfsrc="{{asset('storage/app/public/profileImages/' . $bid[$index]->Tutor->profile_Image)}}" alt="" style="display:none;visibility:hidden;"></a>
@endif
</div>
<!-- Name -->
<div class="freelancer-name">
<h4><a href="">{{$bid[$index]->Tutor->name}}</a></h4>
<!-- Details -->
<strong><span>Subject:</span></strong>
@foreach($bid[$index]->Tutor->subject as $sub)
<strong><span>{{\App\Models\Subject::find($sub->pivot->subject_id)->subject}}</span></strong>
@endforeach
<!-- Rating -->
<div class="freelancer-rating">
<div class="star-rating" data-rating="{{$bid[$index]->Tutor->rating}}"></div>
</div>
<!-- Bid Details -->
<ul class="dashboard-task-info bid-info">
<li><strong id="{{ $bid[$index]->id}}">PKR:{{$bid[$index]->bid_price}}</strong><span>Fixed Price</span></li>
<li><strong>Days:{{$bid[$index]->days}}</strong><span>Delivery Time</span></li>
</ul>
<!-- Buttons -->
<div class="buttons-to-right always-visible margin-top-25 margin-bottom-0" onclick=" indexID({{$i}})">
<a href="#small-dialog-1"
onclick="moveprice({{ $bid[$index]->id}}), payment({{ $bid[$index]->id}})" class="popup-with-zoom-anim button ripple-effect"
@foreach($payments as $pay) @if($bid[$index]->post_id == $pay->post_id) id="hide" @endif @endforeach><i class="icon-material-outline-check "></i> Accept Bid</a>
{{-- @dd($bid->id)--}}
<a href="{{url('student-messages',$bid[$i]->id)}}" class=" button dark ripple-effect"><i class="icon-feather-mail"></i> Send Message</a>
<a id="btn" class="button gray ripple-effect ico" onclick="removeBid({{$bid[$index]->id}})" title="Remove Bid" data-tippy-placement="top"><i class="icon-feather-trash-2"></i></a>
</div>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
</div>
<span style="visibility: hidden">{{$i++}}</span>
@endif
@endforeach
@php
$index++;
@endphp
@endforeach
当我运行代码错误将发生偏移量 1 我需要在刀片中打印所有数据 如何打印数组索引明智
解决方案
你应该使用关系,如果你想显示投标人列表,那么在投标人模型中
应用程序/bidders.php
public function post()
{
return $this->belongsTo('App\Post');
}
假设您在表结构中具有适当的标准来映射 eloquent。
那么你需要改变你的投标人功能如下
public function bidders()
{
$payments = \App\Models\Payment::get();
$bidders = Bidder::whereHas('post', function($post) {
return $post->where('user_id','=',Auth::user()->id);
//Provides a filter for bidders, post which are related to user
})->with('post')->get();
return view('user.student.student-post-bidders',compact('bidders', 'payments'));
}
并且刀片上的投标人应该只有一个循环,如下所示。
Here is your answer why array is not appearing in blade check in comments for json_encode
@foreach($bidders as $bid)
{{$bid->id}} //gives you bid parameters
{{json_encode($bid->post)}} //this will get all the post details array/object is not printable on blade so encoded to json just for display purpose
//suppose you need only name from post then
{{$bid->post->name}};
@endforeach
这将降低您的代码长度,并由于急切加载和更少的循环而提高性能。还删除了诸如创建数组和维护索引之类的东西,以及刀片上的 if 条件以进行 id 检查。
而已。编码快乐!!
推荐阅读
- laravel - 将 [type] 添加到可填充属性以允许在将类名插入表时出现质量分配错误
- java - SQL 服务器突然挂起 - 所有数据库连接都处于活动状态但没有响应 - SQL Server 2016
- reactjs - 你可以从 Next.js 中的 getStaticPaths() 传递非字符串对象吗?
- java - 使用地图时的编码字符串
- python-3.x - 如何获取存储库中的所有提交以及相应的分支名称?
- c# - 如何从您单击的链接按钮中获取字符串?
- proxy - 企业代理背后的开发空间
- kubernetes - Kubernetes中的VPA和HPA(Auto Scaling)可以一起使用吗?
- matlab - 如何错过 pcolor 图中的矩阵元素?
- algorithm - 在两个单元格之间找到网格空间的算法