【发布时间】:2018-12-02 02:47:14
【问题描述】:
当我在刀片中输出查询结果时,数字字段值作为数组返回,但如果您查看原始记录,该值是 1 或 0。
Laravel 版本:5.4.36
产品控制器:
$products = Product::select('id', 'hidden')->take(2)->get();
return view('products.index', compact('products'));
如果我将 ->toArray() 附加到查询中,问题就会消失。
但我不应该(我认为)。
产品型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
}
数据结构:
id: INT(10)
hidden: TINYINT(1)
刀片视图:
@foreach($products as $product)
id: {{$product['id']}}<br>
@if(is_array ($product['hidden']))
hidden (array): <?=count($product['hidden'])?><br>
@endif
record set: {{$product}} <br>
@endforeach
输出
id: 7339
hidden (array): 0
record set: {"id":7339,"hidden":0}
id: 7340
hidden (array): 0
record set: {"id":7340,"hidden":1}
【问题讨论】:
-
请发布
Product模型。 -
但是
{{$product['hidden']}}有效吗? -
no
{{$product['hidden']}}返回:htmlspecialchars() 期望参数 1 是字符串,给定数组 -
添加产品型号
-
{{ dd($product->getAttributes()) }}的结果是什么?
标签: arrays laravel laravel-blade htmlspecialchars