所以我是一个Manifest,您可以在其中添加多个客户,生成等,并将其作为数组保存在数据库中
现在我正在创建一个页面,您可以在其中查看我希望它看起来像这样的所有清单信息
如果我尝试执行此类操作{{ $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>
的print_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
答案 0 :(得分:0)
您可以这样做:
@foreach($manifets as $manifestos)
@for ($counter = 0; $counter < sizeof($manifestos['customer_name']); $counter++)
<tr>
<td>
<input type="text" class="form-control" name="nameoffield" value="{{ $manifestos['customer_name'][$counter] }}" />
</td>
<td>
<input type="text" class="form-control" name="nameoffield" value="{{ $manifestos['otherfields'][$counter]}}" />
</td>
//more td here depending on the fields you want to display
</tr>
@endfor
@endforeach
答案 1 :(得分:0)
问题是您在数据库中存储数组(在PHP中是一个对象)。 这会使您在检索数据时无法识别表单。
在将数组存储到数据库之前,应将其序列化。它会将数据保存为一种JSON字符串。
serialize($array_var_name);
为了让阵列恢复,您可以使用
反序列化($ data_var);
现在您可以像往常一样使用数组