【问题标题】:How to display array data from MySql in laravel如何在 laravel 中显示来自 MySql 的数组数据
【发布时间】: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


【解决方案1】:

你可以这样做:

@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

【讨论】:

  • 我厌倦了,但我得到了尝试获取非对象的属性
  • 你能在你的帖子中显示它以及错误指向哪一行
  • 抱歉,这是一个数组。让我更新我的代码。您的代码中还有一个额外的
  • 当 foreach 应该只输出 2 并且输入值为空白时,不输出 4 tr's
  • 您可以发布您的更新代码以便我们查看吗?
【解决方案2】:

问题是您将数组(在 PHP 中是一个对象)存储在数据库中。 当您检索数据时,这会使表单无法识别。

您应该在将数组存储到数据库之前对其进行序列化。它将数据保存为一种 JSON 字符串。

serialize($array_var_name);

进一步获取数组,您可以使用

反序列化($data_var);

现在你可以像往常一样使用数组了

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签