Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 拉维二列表_Laravel - Fatal编程技术网

Laravel 拉维二列表

Laravel 拉维二列表,laravel,Laravel,我有这样的数据库 产品表 | id | name | | 1 | Product 1 | 可加工 | id | name | | 1 | Machine 1 | | 2 | Machine 2 | 准稳态 | Product_id | Machine_id | Keys | Value | | 1 | 1 | Zone 1 | 10 | | 1 | 2 | Zone 1 | 20

我有这样的数据库

产品表

| id | name      |
| 1  | Product 1 |
可加工

| id | name      |
| 1  | Machine 1 |
| 2  | Machine 2 |
准稳态

| Product_id | Machine_id | Keys   | Value |
|     1      |      1     | Zone 1 |  10   |
|     1      |      2     | Zone 1 |  20   |
|     1      |      2     | Zone 2 |  20   |
当我在ParamsTable上找到by product_id时,我在视图上想要什么:

| Keys       | Machine 1  | Machine 2   |
| Zone 1     |     10     |     20      |
| Zone 2     |      -     |     20      |
这是我的控制器

public function show(Product $product){
  $data = ParameterSetting::with('product', 'machine')->where('product_id', $product->id)->get();

  return view('production.parameter.view', [
    'machines' => $data->unique('machine.name')->pluck('machine.name'),
    'params' => $data->unique('keys')->pluck('keys'),
    'data' => $data,
  ]);
}
但我不知道最好的方法是什么。
应该使用什么样的有说服力的关系以及如何循环它。谢谢你。

我认为你不是在寻找一段感情。您只需按照所需的格式重新排列数据,并将其传递到视图中。下面是一个例子:

// rearrange data into $result array
$result = [];
foreach($data as $param){
  $result[$param->keys][$param->machine->name] = $param->value;
}

// pass the new array into the view
return view('production.parameter.view', [
    'machines' => $data->unique('machine.name')->pluck('machine.name'),
    'params' => $data->unique('keys')->pluck('keys'),
    'data' => $data,
    'result' => result
  ]);
哪个将创建此数组

// $result content
[
  'Zone 1' => [
    'Machine 1' => 10,
    'Machine 2' => 20,
  ],
  'Zone 2' => [
    'Machine 2' => 20
  ]
]

因此,在您的视图中,您可以循环使用它来填充表

@foreach($result as $keys => $values)
  <tr>
    <td> {{ $keys }} </td>
    @foreach($machines as $machineName)
      <td> {{$values[$machineName] ?? '-' }} </td>
    @endforeach
  </tr>
@endforeach

@foreach($result as$keys=>$value)
{{$keys}}
@foreach($machineName形式的机器)
{{$values[$machineName]??'-'}}
@endforeach
@endforeach

我不认为你在寻找关系。您只需按照所需的格式重新排列数据,并将其传递到视图中。下面是一个例子:

// rearrange data into $result array
$result = [];
foreach($data as $param){
  $result[$param->keys][$param->machine->name] = $param->value;
}

// pass the new array into the view
return view('production.parameter.view', [
    'machines' => $data->unique('machine.name')->pluck('machine.name'),
    'params' => $data->unique('keys')->pluck('keys'),
    'data' => $data,
    'result' => result
  ]);
哪个将创建此数组

// $result content
[
  'Zone 1' => [
    'Machine 1' => 10,
    'Machine 2' => 20,
  ],
  'Zone 2' => [
    'Machine 2' => 20
  ]
]

因此,在您的视图中,您可以循环使用它来填充表

@foreach($result as $keys => $values)
  <tr>
    <td> {{ $keys }} </td>
    @foreach($machines as $machineName)
      <td> {{$values[$machineName] ?? '-' }} </td>
    @endforeach
  </tr>
@endforeach

@foreach($result as$keys=>$value)
{{$keys}}
@foreach($machineName形式的机器)
{{$values[$machineName]??'-'}}
@endforeach
@endforeach