dimanche 29 octobre 2017

Leaving reviews on multiple products and Laravel relations

I have order system and when user order some product he then can leave feedback for the product.

My issue is when there are multiple products in same order. I want to place button on each product leave feedback.

In my controller I have this

public function orderView($orderId)
{       
    $order = Order::where('order_id', $orderId)->where('status', 1)->where('user_id', Auth::user()->user_id)->first();
    $reviews = Review::where('user_id', Auth::user()->user_id)->get();        

    return View::make('order_details', [
        'order' => $order,
        'reviews' => $reviews
    ]);
}

The function query the orders table and reviews table. Then on the page I'm trying to do this

@foreach($reviews as $review)   
    @if($item->product_id == $review->product_id)                                   
        @if($review->rating_published == 0)
            <a class="btn btn-warning" href="">Wating for Approval</a>
        @else   
            <a class="btn btn-warning" href="">Edit Review</a>                                  
        @endif                                          
    @else
        <a class="btn btn-warning" href="">Leave Review</a>
    @endif
@endforeach

For now the problem is that if there is no reviews from logged user I don't see "leave review" button on page.

My review model have

public function user()
{
    return $this->belongsTo('App\User', 'user_id', 'user_id');
}
public function item()
{
    return $this->belongsTo('App\Product', 'product_id','product_id');
}

Product model

public function reviews()
{
    return $this->hasMany('App\Review', 'product_id'); 
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire