【问题标题】:Grouping related models using Laravel 5.1 blade and HTML使用 Laravel 5.1 刀片和 HTML 对相关模型进行分组
【发布时间】:2016-06-11 00:34:56
【问题描述】:

我发现很难使用 html 表将相关模型分组在一起。我试图提出的系统是一个投标系统。多个用户可以对一个产品以及多个产品进行投标。当所有者查看他的产品时,应根据产品名称对其进行分组。

假设一个用户发布了 2 个产品,而其他 4 个用户对这些产品出价,用户的出价应该在产品名称下分组,但我得到的是它在不同的表下分组. 这是我的刀片视图:

<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);
    }

请查看随附的屏幕截图,以了解我想要实现的目标和得到的结果。

这就是我想要实现的目标:

这就是我现在得到的错误

【问题讨论】:

    标签: html model laravel-5.1 grouping laravel-blade


    【解决方案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);
        }
    

    【讨论】:

      【解决方案2】:

      我不完全确定这是否是您想要的,但这是我想出的

      @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
      

      【讨论】:

      • 谢谢@vshotarov。感谢您的热情贡献。我能够使用下面发布的逻辑完成它。
      猜你喜欢
      • 2016-03-16
      • 2016-07-07
      • 1970-01-01
      • 2015-11-08
      • 2016-03-11
      • 2015-10-21
      • 1970-01-01
      • 2015-12-01
      • 2015-08-10
      相关资源
      最近更新 更多