【问题标题】:Nested Table Loop Laravel 5.4嵌套表循环 Laravel 5.4
【发布时间】:2017-11-17 16:39:09
【问题描述】:

我在从我的 Laravel 集合中编写一些嵌套的 HTML Tables 时遇到了一些麻烦。

我的模特

public static function offers($userId)
{
    return DB::table('users_vehicles')
    ->join('dealer_offers', 'users_vehicles.id', '=', 'dealer_offers.vehicle_id')
      ->where('user_id', '=', $userId)
      ->select('users_vehicles.*', 'dealer_offers.*');
}

我的控制器

public function dash()
{

    $userCars  = UserVehicles::offers(Auth::user()->id)->get();

    return view('customer.dash', compact('userCars'));
}

我的收藏结果

在我的视图中刀片模板

@foreach( $userCars->chunk(1) as $userCarChunk)
    <div id="no-more-tables">
        <table  class="table table-bordered table-hover">
            <caption>Offers for My Mazda 326 </caption>
            <thead> 
                <tr> 
                    <th class="numeric">DEALER RATING</th> 
                    <th class="numeric">AMOUNT </th>
                    <th class="numeric"> 3 DAYS To Accept </th>
                </tr> 
            </thead> 
            <tbody>
            @foreach( $userCarChunk as $car)
                <tr> 
                    <td data-title="First Name">
                        <div class="rating">
                            <label>
                                <input type="radio" name="rating" value="5" title="5 stars"> 5
                            </label>

                        </div>
                    </td> 
                    <td data-title="Last Name"> 55 000 </td>
                    <td data-title="Username">
                        <a href="{{ route('customer.eval.show', 1) }}">Accept</a>
                    </td> 
                </tr>
                @endforeach
            </tbody> 
        </table>
    </div>
@endforeach

前端视图

问题

如何为所有用户车辆创建一个嵌套的 HTML 表,以便我可以写下该特定车辆的车辆名称和所有报价?

我希望我的问题很清楚。

【问题讨论】:

  • 你能分享你的表结构吗?乍一看,您似乎可以通过使用雄辩的关系而不是使用 DB::table('users_vehicles') 来解决您的问题(这是您的代码出现问题的明确迹象)。
  • 表结构。不是 ORM 模型。我将假设您按照关系顺序有表user_info &lt;--&gt; user &lt;--&gt; user_vehicles &lt;--&gt; vehicle_offers &lt;--&gt; dealer?如果是这种情况,那么您应该执行$iteratee = UserVehicles::with("vehicle_offers")-&gt;whereHas("vehicle_offers")-&gt;where("user_id",auth()-&gt;id()) 并为每辆车打印车辆,然后打印相关报价表。任何地方都不需要静态的东西。
  • You mean This 另外,我现在试试你的建议

标签: php laravel laravel-5 laravel-5.4


【解决方案1】:

试试这样:

<div id="no-more-tables">
    <table  class="table table-bordered table-hover">
        <caption>Offers for My Mazda 326 </caption>
        <thead> 
            <tr> 
                <th class="numeric">DEALER RATING</th> 
                <th class="numeric">AMOUNT </th>
                <th class="numeric"> 3 DAYS To Accept </th>
            </tr> 
        </thead> 
        <tbody>
            @foreach( $userCars->chunk(1) as $userCarChunk)
                @foreach( $userCarChunk as $car)
                    <tr> 
                        <td data-title="First Name">
                            <div class="rating">
                                <label>
                                    <input type="radio" name="rating" value="5" title="5 stars"> 5
                                </label>

                            </div>
                        </td> 
                        <td data-title="Last Name"> 55 000 </td>
                        <td data-title="Username">
                            <a href="{{ route('customer.eval.show', 1) }}">Accept</a>
                        </td> 
                    </tr>
                @endforeach
            @endforeach
        </tbody> 
    </table>
</div>

【讨论】:

  • 我确实尝试过这样做。但是,它没有用。因为我想在Table Caption::&lt;caption&gt;Offers for My {{ }} &lt;/caption&gt;上输出车辆名称
  • 模型之间有关系吗??
  • this link以及这个model link
  • 让我们继续聊天discussion !
【解决方案2】:

回答

我的模特UserVehicles.php

public function offers() {
    return $this->hasMany(DealerOffers::class, 'vehicle_id');
}

我的控制器CustomerController.php

$offersToCustomer = UserVehicles::with('offers')->get();

仪表板Blade template

@foreach( $offersToCustomer as $offer)
    <div id="no-more-tables_">
        <table  class="table table-bordered table-hover">
            <caption>Offers for My :: <b style="color: #00BFF0"> {{ $offer->vehicle_make }} {{ $offer->vehicle_model }}  </b> </caption>
            <thead>
                <tr>
                    <th class="numeric">DEALER RATING</th>
                    <th class="numeric">AMOUNT REQUESTED </th>
                    <th class="numeric">AMOUNT OFFERED </th>
                    <th class="numeric"> Expires :: {{ Carbon\Carbon::parse($offer->offer_expiry)->format('d-m-Y') }} </th>
                </tr>
            </thead>
                <tbody>
                @foreach( $offer->offers as $subOffer)
                    <tr>
                        <td data-title="DEALER RATING">
                            <input id="rating" name="rating" class="rating rating-loading" value="3" data-min="0" data-max="5" data-step="1"  />
                        </td>
                        <td data-title="AMOUNT REQUESTED">R  {{ number_format($offer->vehicle_amount_asked, 2, '.', ' ') }} </td>
                        <td data-title="AMOUNT OFFERED">R  {{ number_format($subOffer->offer_amount, 2, '.', ' ') }} </td>
                        <td data-title="ACTIONS">
                            <button data-offer_id="{{ $offer->id }} " data-vehicle_id="{{ $subOffer->vehicle_id }}"
                                            class="btn btn-xs btn-success accept-offer">Accept</button>
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    @endforeach

输出

【讨论】:

    猜你喜欢
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    • 2016-06-11
    • 1970-01-01
    • 2017-11-13
    • 2021-05-05
    • 2019-04-22
    相关资源
    最近更新 更多