【发布时间】:2018-04-14 13:01:01
【问题描述】:
所以我是一个清单,您可以在其中添加多个客户、生产等,并将其作为数组保存在数据库中
现在我正在制作一个页面,您可以在其中查看所有清单信息我希望它看起来像这样
如果我尝试做这样的事情{{ $manifest->customer_name }} 我收到错误htmlspecialchars() expects parameter 1 to be string, array given 因为它是一个数组我只是无法显示数组数据
获取正确清单的控制器代码
public function view($id) {
$manifests = Manifest::where('id', $id)->where('user_id', Auth::user()->id)->firstOrFail();
return view('users.manifest.view', compact('manifests'));
}
查看 - 通常我只会输入 {{ $manifets->customer_name }} 但我不能,因为它是一个数组
<table class="table table-striped">
<thead>
<tr>
<th width="15%">Customer</th>
<th width="15%">Produce</th>
<th width="15%">Task</th>
<th width="15%">Units</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="text" class="form-control" value="{{ $manifests->customer_name }}" />
</td>
</tr>
</tbody>
</table>
打印_r
App\Manifest Object
(
[table:protected] => manifest
[fillable:protected] => Array
(
[0] => user_id
[1] => date
[2] => driver_name
[3] => truck_number
[4] => run_number
[5] => customer_name
[6] => produce
[7] => task
[8] => units
)
[casts:protected] => Array
(
[customer_name] => array
[produce] => array
[task] => array
[units] => array
)
[connection:protected] => mysql
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 8
[user_id] => 1
[date] => 2017-11-02
[driver_name] => Harry Oberlander
[truck_number] => 7989
[run_number] => 8
[customer_name] => ["evergreen","Surplus"]
[produce] => ["Apples","Meat"]
[task] => ["Pick Up","Delivery"]
[units] => ["3 skids","1"]
[created_at] => 2017-11-02 04:49:49
[updated_at] => 2017-11-02 04:49:49
)
[original:protected] => Array
(
[id] => 8
[user_id] => 1
[date] => 2017-11-02
[driver_name] => Harry Oberlander
[truck_number] => 7989
[run_number] => 8
[customer_name] => ["evergreen","Surplus"]
[produce] => ["Apples","Meat"]
[task] => ["Pick Up","Delivery"]
[units] => ["3 skids","1"]
[created_at] => 2017-11-02 04:49:49
[updated_at] => 2017-11-02 04:49:49
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
这是更新的代码
@foreach($manifests as $manifest)
<tr>
<td>
<input type="text" class="form-control" value="{{ $manifest['customer_name'] }}" />
</td>
<td>
<input type="text" class="form-control" value="{{ $manifest['produce'] }}" />
</td>
<td>
<input type="text" class="form-control" value="{{ $manifest['task'] }}" />
</td>
</tr>
@endforeach
【问题讨论】:
-
你的数组是一个循环,这样你就可以以这种方式呈现你的数据
-
@hungrykoala 介意发布一个例子吗?
-
我不能不知道你的显示器代码
-
@hungrykoala 让我知道这是否是您要查找的代码
-
你可以做一个
print_r($manifests);并在这里显示结果
标签: php mysql arrays laravel laravel-5