2016-02-27 142 views
1

相關的模型,我發現很難將相關模型一起使用HTML表格。我試圖想出的系統是一個招投標系統。多個用戶可以對產品進行出價,也可以對多個產品進行出價。所有者查看其產品時,應根據產品名稱對其進行分組。分組使用Laravel 5.1刀片和HTML

讓我們假設某個用戶的帖子2級的產品,和其他4個用戶對這些產品,用戶的投標應在產品名稱進行分組競標,但我什麼,我得到的是每個IT部門在不同的表。 這是我的看法刀片:

<DIV style="color:#0000FF"> 
        @foreach($myBuyers as $myQuotedProducts) 
          @foreach($product as $products) 
           @if($products->id === $myQuotedProducts->product_id) 
            @if(count($products->productname) > 1) 
           <div style="color:#0000FF"> 
            <p>{!! $products->productname !!}</p> 
            <TABLE> 
            <TH>SELECT</TH><TH>PRODUCT</TH><TH>COMMENT</TH><TH>PRICE</TH> 
             <TR><TD>{!! Form::radio('selectedButton' .$myQuotedProducts->product_id, $myQuotedProducts->product_id) !!}</TD><TD>{!! $products->productname !!}</TD><TD>{!! $myQuotedProducts->comments !!}</TD><TD>{!! $myQuotedProducts->price !!}</TD></TR> 
            </TABLE> 
           </div> 
           @else 
            <div style="color:#0000FF"> 
            <p>{!! $products->productname !!}</p> 
            <TABLE> 
            <TH>SELECT</TH><TH>PRODUCT</TH><TH>COMMENT</TH><TH>PRICE</TH> 
             <TR><TD>{!! Form::radio('selectedButton' .$myQuotedProducts->product_id, $myQuotedProducts->product_id) !!}</TD><TD>{!! $products->productname !!}</TD><TD>{!! $myQuotedProducts->comments !!}</TD><TD>{!! $myQuotedProducts->price !!}</TD></TR> 
            </TABLE> 
           </div> 
           @endif 
           @endif 
          @endforeach 
         @endforeach 

       </DIV> 

這是我的控制器:

public function index() 
    { 
     $product = Product::all(); 
     $myBuyers = BiddingComments::where('user_id', '=', Auth::user()->id)->orderBy('price', 'DESC')->get(); 

     return view('buyers.index') 
     ->with('product', $product) 
     ->with('myBuyers', $myBuyers); 
    } 

敬請參閱所附截圖要非常熟悉什麼我想達到什麼我得到。

這就是我想要達到的目標:

This is what I want to achieve

這就是我得到現在這是不對的

enter image description here

回答

1

我不完全知道這是什麼你想要的,但這裏是我想出了

@foreach($product as $products) 
    <div style="color:#0000FF"> 
     <p>{!! $products->productname !!}</p> 
     <TABLE> 
     <TH>SELECT</TH><TH>PRODUCT</TH><TH>COMMENT</TH><TH>PRICE</TH> 
     @foreach($myBuyers as $myQuotedProducts) 
      @if($products->id == $myQuotedProducts->id) 
       <TR> 
        <TD>{!! Form::radio('selectedButton' .$myQuotedProducts->product_id, $myQuotedProducts->product_id) !!}</TD> 
        <TD>{!! $products->productname !!}</TD><TD>{!! $myQuotedProducts->comments !!}</TD> 
        <TD>{!! $myQuotedProducts->price !!}</TD> 
       </TR> 
      @endif 
     @endforeach 
     </TABLE> 
    </div> 
@endforeach 
+0

由於@vshotarov。我感謝你的熱烈貢獻。我能夠用下面的邏輯完成它。 – kehinde

+0

我很高興你有它的工作! – vshotarov

1

感謝所有。這是讓我到達目的地的解決方法。我很感謝大家爲此做出的貢獻。

這是葉片視圖:

<DIV style="color:#0000FF"> 
        @foreach($myBuyers as $myQuotedProducts) 
          @foreach($product as $products) 
           @if($products->id === $myQuotedProducts->product_id) 
            @if($myQuotedProducts->user_id === Auth::user()->id) 
            <TABLE> 
             {!! $products->productname !!} | {!! count($myQuotedProducts->product_id) !!} 
              <TH>COMMENTS</TH><TH>PRICE</TH> 
              @foreach($myBuyersCount as $countingBuyers) 
               @if($countingBuyers->product_id === $myQuotedProducts->product_id) 
                <TR><TD>{!! $countingBuyers->comments !!} </TD><TD>{!! $countingBuyers->price !!} </TD></TR> 
               @endif 
              @endforeach 

            </TABLE> 
            @endif 
           @endif 
          @endforeach 
         @endforeach 
       </DIV> 

這是控制器:

public function index() 
    { 
     $product = Product::all(); 
     $myBuyers = BiddingComments::where('user_id', '=', Auth::user()->id)->orderBy('price', 'DESC')->groupBy('product_id')->get()->all(); 
     $myBuyersCount = BiddingComments::where('user_id', '=', Auth::user()->id)->orderBy('price', 'DESC')->get(); 

     return view('buyers.index') 
     ->with('product', $product) 
     ->with('myBuyersCount', $myBuyersCount) 
     ->with('myBuyers', $myBuyers); 
    }