简体   繁体   English

如何在一个视图中从两个表中获取多个列数据

[英]How to get multiple column data from two tables in one view

I am trying to get data from two tables(brands and raw_materials), connected by a foreign key (brand_id). 我试图从通过外键(brand_id)连接的两个表(品牌和raw_materials)中获取数据。 A brand can have multiple raw materials. 一个品牌可以有多种原材料。 I want it all displaying under its respective brand on one page. 我希望所有内容都以各自的品牌显示在一页上。 Instead, I get the results displayed separately. 相反,我将结果分开显示。

Using Laravel 5.7 and Eloquent. 使用Laravel 5.7和Eloquent。

My Tables
[brands]
id    name
----------
1   Apple 
2   Colgate
3   LintX

[raw_materials]
id    name        brand_id      quantity
-----------------------------------------
1    50g pouch        1        200
2    10g laminate     3        320
3    75mm oil         1        500
4    Mint Rubber      3        400
5    Fire pouch       2        550
6    400 String       1        500
7    90g pouch        2        200



PageController 的PageController

$brandData = DB::table('brands')
            ->join('raw_materials','raw_materials.brand_id','brands.id')
            ->select('brands.id as brand_id','brands.name as brand_name','raw_materials.name as raw_material','raw_materials.quantity')
            ->orderBy('brands.id')
            ->get();<br>
return view('rawmaterials.index', [
            'brandData'=>$brandData,
        ]);



View 视图

@foreach ($brandData as $brand)
<p>Brand Name: {{$brand->brand_name}}</p>
<p>{{$brand->raw_material}} | Quantity: {{$brand->quantity}}</p>
<br>
@endforeach

Expected Results: 预期成绩:

Brand Name: Apple 品牌名称:苹果
50g pouch | 50克小袋| Quantity: 200 数量:200
75mm oil | 75mm油| Quantity: 500 数量:500
400 String | 400弦| Quantity: 500 数量:500

Brand Name: Colgate 品牌名称:高露洁
Fire pouch | 火袋| Quantity: 550 数量:550
75mm oil | 75mm油| Quantity: 500 数量:500
400 String | 400弦| Quantity: 500 数量:500

next brand ..


Actual Result: 实际结果:

Brand Name: Apple 品牌名称:苹果
50g pouch | 50克小袋| Quantity: 200 数量:200

Brand Name: Apple 品牌名称:苹果
75mm oil | 75mm油| Quantity: 500 数量:500

Brand Name: Apple 品牌名称:苹果
400 String | 400弦| Quantity: 500 数量:500

Brand Name: Colgate 品牌名称:高露洁
Fire Pouch | 消防袋| Quantity: 550 数量:550

And so on..

This will work 这会起作用

$brandData = \DB::table('brands')->orderBy("brands.id")->get();

foreach ($brandData as $brand) {
    $brand->raw_materials = \DB::table("raw_materials")->where("brand_id", $brand->id)->get();
}

return view('rawmaterials.index', [
            'brandData'=>$brandData
        ]);

Then in your view you'll use nested like this ... 然后在您的视图中,您将使用像这样的嵌套...

@foreach ($brandData as $brand)
<p>Brand Name: {{$brand->name}}</p>
@foreach($brand->raw_materials as $raw)
<p>{{$raw->name}} | Quantity: {{$raw->quantity}}</p>
@endforeach
<br>
@endforeach

That should get the job done. 那应该完成工作。

On the other hand if you would like to use Eloquent rather you have to set the relationships in both Brand model and RawMaterial model as follows, assuming Brand and RawMaterial models are directly in the app folder 另一方面,如果您想使用Eloquent,则必须按照如下方式在Brand模型和RawMaterial模型中设置关系,假设Brand和RawMaterial模型直接位于应用程序文件夹中

class Brand extends Model
{
    public function raw_materials()
    {
        return $this->hasMany('App\RawMaterial');
    }
}

class RawMaterial extends Model
{
    public function brand()
    {
        return $this->belongsTo('App\Brand');
    }
}

Then in your controller you simply do this ... 然后,只需在控制器中执行此操作即可...

$brandData = \App\Brand::with("raw_materials")->orderBy("id")->get();

No need to change the blade syntax in your .blade file as the same foreach loop applies 无需更改.blade文件中的刀片语法,因为适用于相同的foreach循环

You can create a relationship first: 您可以先创建一个关系:

function raw_materials() {
     return $this->hasMany(RawMaterial.class);
}

In your controller, use the with() function like so: 在您的控制器中,使用with()函数,如下所示:

$brandData = Brand::with('raw_materials')->all();
return view('rawmaterials.index', [
    'brandData'=>$brandData
]);

Then in your view, you can get the raw_materials table data: 然后在您的视图中,可以获取raw_materials表数据:

@foreach($branData as $brand)
   <h3>{{ $brand->name }}</h3>
   @foreach($brand->raw_materials as $raw_material)
       <div>{{ $raw_material->name }}</div>
       <div>{{ $raw_material->quantity }}</div>
   @endforeach
@endforeach

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM