选择并检查用户是否存在于表中,而laravel刀片中没有foreach [英] Selecting and check if user exist in table without foreach in laravel blade
问题描述
我不知道该如何追踪.用户可以查看其订单的详细信息.我希望为用户提供一个按钮,以便能够对他购买的产品进行评论.
I can't figured it out how can I make following. User can see details of his order. I want to make button for a user to be able to leave a review of the product which he purchased.
当他离开产品评论时,我想将按钮从提交评论"更改为编辑评论".问题是,如果他购买了2件或更多商品,该怎么办?我希望能够分别对每个项目进行评论.
When he left review for a product I want to change the button from "submit review" to "edit review". Problem is how to do this if he purchased 2 or more items? I want to be able to leave review for each item separately.
在控制器中,我具有以下功能:
In the controller I have this function:
public function orderView($orderId)
{
$order = Order::where('order_id', $orderId)->where('user_id', Auth::user()->user_id)->first();
$reviews = Review::where('user_id', Auth::user()->user_id)->first();
return View::make('site.users.orders_view', [
'order' => $order,
'reviews' => $reviews
]);
}
因此,在这里,我为经过身份验证的用户选择他的订单和评论.我在视图上的提交评论",编辑评论"上显示按钮的位置
So, here I select his orders and reviews for authenticated user. Where I show button on the view "submit review", "edit review" I've tried this
@forelse($reviews as $review)
@if($review->product_id == $item->product_id)
@if($review->rating_published == 0)
<a>Waiting</a>
@else
<a href="">Edit</a>
@endif
@else
<a href="">Add</a>
@endif
@empty
<a href="">Add</a>
@endforelse
如果用户按该顺序购买了2种产品,并且仅留给其中一种产品,则两个按钮均更改为Edit Review
.
If user has 2 products in that order and left only for one of them review both buttons are changed to Edit Review
.
我想做的是,如果用户按该顺序购买了2种产品,并且对其中1种产品进行审查,则需要一个按钮编辑审查"和一个提交审查"
What I want to do is if user has 2 products in that order and review for 1 of the products to have one button "edit review" and one "submit review"
推荐答案
在产品"模型和评论"模型中添加关系之后,您可以按如下方式编辑Controller代码:
After adding the relationships in the Product model and Review model you can edit the Controller code as follow :
public function orderView($orderId)
{
$order = Order::where('order_id', $orderId)->where('status', 1)
->where('user_id', Auth::user()->user_id)
->first();
$productIds = $order->getOrderProductIds();
$reviews = Review::with('item')
->whereHas('item', function($q) use($productIds){
$q->whereIn('product_id', $productIds);
})
->where('user_id', Auth::user()->user_id)
->get();
return View::make('site.users.orders_view', [
'order' => $order,
'reviews' => $reviews
]);
}
然后在产品模型中添加此hepler方法:
Then add this hepler method in the Product model :
public static function hasReview($reviews, $product_id)
{
foreach ($reviews as $review) {
if ($review->item->product_id == $product_id) {
return $review;
}
}
return null;
}
在视图中,您应该这样做:
And in the view you should do this :
<?php $review = \App\Product::hasReview($reviews, $item->product_id)?>
@if($review != null)
@if($review->rating_published == 0)
<a class="btn btn-warning btn-xs">Waiting for Approval ID: {{$item->product_id }}</a>
@else
<a class="btn btn-warning btn-xs" href="{{ URL::to('/users/review/' . $item->product_id . '?_token=' . csrf_token()) }}">Edit Review ID: {{$item->product_id }}</a>
@endif
@else
<a class="btn btn-warning btn-xs" href="{{ URL::to('/users/review/' . $item->product_id . '?_token=' . csrf_token()) }}">Leave Review ID: {{$item->product_id }}</a>
@endif
PS:考虑添加:
- 产品与订单之间的多对多关系=>它将在很多事情上帮助您,尤其是查询.
- Helper类中的女巫将包含
hasReview
之类的辅助方法.
- Many to Many relationship between Product and Order => it will helps you in many things especially queries.
- Helper class witch will contains the helper methods like
hasReview
这篇关于选择并检查用户是否存在于表中,而laravel刀片中没有foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!