关于如何在 Laravel 5.5 中的 3 个不同 SQL Table 视图中填充我的 HTML Table 的建议?

Suggestions on how I populate my HTML Table in the view from 3 different SQL Tables in Laravel 5.5?

希望你能帮我解决这里的问题。我想要做的是根据 3 个不同的 SQL table 中的查询数据在我的视图中填充我的 table。

如何填写:

根据收集到的数据:

MySQL 查询:

-- Ordered Query

SELECT abstract_supplier.supplier,abstract_supplier.canvasser_name,abstract_supplier.canvasser_department, abstract_items.particulars, abstract_items.qty,abstract_items.unit,abstract_price.unit_price, abstract_price.total_price 
    -> FROM abstract_items
    -> RIGHT JOIN abstract_price ON abstract_price.item_id = abstract_items.id
    -> LEFT JOIN abstract_supplier ON abstract_supplier.id = abstract_price.supplier_id;

我的控制器:

 public function show($id)
    {
        $abstract = AbstractModel::find($id);
        $office = Office::all();
        $pr_item = PurchaseRequestItemModel::all()->where('pr_form_number',$abstract->pr_number);
        $grand_total = $pr_item->sum('pr_estimated_cost');

        $abstract_items = abstractitemmodel::all()->where('abstract_id',"=",$abstract->id);

        $query= DB::table('abstract_supplier')
        ->paginate(3);

        // dd($query);
        return view('abstract.abstract-form',compact('abstract','abstract_items','pr_item','grand_total','office','query','supp_query'));
    }

我的看法

<div>
    <table class="table table-responsive table-bordered table-condensed">
      <thead class="text-center">
        <tr class="center-t">
            <th  rowspan="3" class="col-xs-3">Particulars</th>
            <th rowspan="3" class="col-xs-1">Qty</th>
            <th  rowspan="3" class="col-xs-1">Unit</th>

            @php $counter = 0; @endphp
            @foreach($query as $key => $suppliername)
            @php $counter++; @endphp
            <th colspan="2" class="col-xs-2">Supplier {{$counter}}</th>     
            @endforeach
            @if($query->count() < 3)
                @for($i = $query->count(); $i < 3; $i++)
                <th colspan="2" class="col-xs-2">Supplier</th>
                @endfor
            @endif

        </tr>
        <tr>

            @foreach($query as $indexKey => $suppliers)
            <td colspan="2" class="col-xs-2 someCell">{{$suppliers->supplier}}</td>
            @endforeach
            @if($query->count() < 3)
                @for($i = $query->count(); $i < 3; $i++)
                <td colspan="2" class="col-xs-2">N/A</td>
                @endfor
            @endif



        </tr>
        <tr class="center-t">

            @foreach($query as $key => $prices)
            <th class="col-xs-1">Price/Unit</th>
            <th class="col-xs-1">Price/Item</th>
            @endforeach
            @if($query->count() < 3)
                @for($i = $query->count(); $i < 3; $i++)
                <th class="col-xs-1">Price/Unit</th>
                <th class="col-xs-1">Price/Item</th>
                @endfor
            @endif

        </tr>
      </thead>
      <tbody>
        @foreach($abstract_items as $key2 => $items)
        <tr>
            <td>{{$items->particulars}}</td>
            <td>{{$items->qty}}</td>
            <td>{{$items->unit}}</td>
            @foreach($query as $price_key => $prices)
            <td  class="text-right"></td>
            <td  class="text-right"></td>
            @endforeach
            @if($query->count() < 3)
                @for($i = $query->count(); $i < 3; $i++)
                <td class="col-xs-1">N/A</td>
                <td class="col-xs-1">N/A</td>
                @endfor
            @endif
        </tr>
        @endforeach
      </tbody>
    </table>
    {{$query->links()}}
</div>

如您所见,我使用 foreach 循环并填充 table 但结果不是我所期望的,我希望您能就如何处理此问题提供见解和建议。

虽然我会重命名您的 AbstractModel 因为它可能会给其他程序员带来严重的混淆并且不遵循约定,但我想我理解您的意图:

class AbstractModel extends Model
{
    // protected $table = 'abstract';
    protected $fillable = [
        'id',
        'created_at',
        'created_by' ,
        'abstract_no',
        'pr_number',
        'proc_details',
        'office',
        'requestor_name'
    ];

    public function offices()
    {
        return $this->hasOne('App\Office', 'id', 'office');
    }

    public function purchaseRequestItemModels()
    {
        return $this->hasOne('App\PurchaseRequestItemModel', 'id', 'pr_number');
    }

}

Class Office extends Model
{

    ...

    public function abstractModels()
    {
        return $this->belongsTo('App\AbstractModel', 'office', 'id');
    }

}

class PurchaseRequestItemModel extends Model
{
    ...

    public function abstractModels()
    {
        return $this->belongsTo('App\AbstractModel', 'pr_number', 'id');
    }

}

然后您可以继续实例化 AbstractModel 并捕获链接的办公室和 PRNumber,如下所示:

$abstract = AbstractModel::find($id);

//These method calls return Objects of the type:
// Office
$abstract->offices();

//PurchaseRequestItemModel
$abstract->purchaseRequestItemModels();

现在,请检查 coding standards 以进一步改进您的语义和代码质量,因为这对将来有很大帮助!

注意: 我知道你可能有其他 classes 互相交谈,如果你像我上面举例说明的那样建立关系,你可以获得相同的结果。

可以找到进一步的关系解释here