Php 如何将数组数据从json字段显示到刀片视图?

Php 如何将数组数据从json字段显示到刀片视图?,php,laravel,Php,Laravel,我创建了一个json字段来存储数组数据 仓库表结构 在仓库模型中,我使用$cast存储阵列数据 protected $casts = [ 'produce_stored' => 'array' ]; 在仓库控制器中-索引方法 $warehouses = DB::table('warehouses') ->join('regions', 'regions.id', '=', 'warehouses.region_id')

我创建了一个json字段来存储数组数据

仓库表结构

在仓库模型中,我使用$cast存储阵列数据

protected $casts = [
    'produce_stored' => 'array'
];
在仓库控制器中-索引方法

$warehouses = DB::table('warehouses')
                ->join('regions', 'regions.id', '=', 'warehouses.region_id')
                ->join('districts', 'districts.id', '=', 'warehouses.district_id')
                ->join('users', 'users.id', '=', 'warehouses.agent_assigned')
                ->select(
                    'warehouses.id',
                    'warehouses.name as warehouseName',
                    'warehouses.ownership_type',
                    'warehouses.capacity',
                    'warehouses.produce_stored',  // json field with array data
                    'regions.name as regionName', 
                    'districts.name as districtName',
                    'users.name as agent_assigned',
                    'warehouses.status'
                    )
                ->get();

return view('admin.warehouses.index', compact('warehouses'));
在刀片视图中

{{ $warehouse->produce_stored }}
这是浏览器中的输出

["1", "2"]
当我尝试在数组中循环以获取产品ID以加载产品名称时

@foreach ($warehouse->produceStored as $produce)
    {{$produce}}
@endforeach
上面的代码抛出了一个错误

Invalid argument supplied for foreach()
如何从Product_存储字段中获取产品ID?

您有两个选择:

 <?php $warehouse->produceStored = json_decode($warehouse->produceStored, false); ?>
2-正如Autista_z在评论中所说:

$warehouses = Warehouse::query()
                ->join('regions', 'regions.id', '=', 'warehouses.region_id')
                ->join('districts', 'districts.id', '=', 'warehouses.district_id')
                ->join('users', 'users.id', '=', 'warehouses.agent_assigned')
                ->select(
                    'warehouses.id',
                    'warehouses.name as warehouseName',
                    'warehouses.ownership_type',
                    'warehouses.capacity',
                    'warehouses.produce_stored',  // json field with array data
                    'regions.name as regionName', 
                    'districts.name as districtName',
                    'users.name as agent_assigned',
                    'warehouses.status'
                    )
                ->get();

注意:如果它们不相同,请用模型名称替换仓库…

问题是,您使用的不是模型,而是DB调用。在他的情况下,演员没有被称为。将
DB::table('warehouses')
替换为模型调用,
\App\Warehouse::select…
(正确的模型放置),现在产品ID已列出。如何检索产品名称?我认为您应该在将其发送到视图之前将其加载到控制器中
$warehouses = Warehouse::query()
                ->join('regions', 'regions.id', '=', 'warehouses.region_id')
                ->join('districts', 'districts.id', '=', 'warehouses.district_id')
                ->join('users', 'users.id', '=', 'warehouses.agent_assigned')
                ->select(
                    'warehouses.id',
                    'warehouses.name as warehouseName',
                    'warehouses.ownership_type',
                    'warehouses.capacity',
                    'warehouses.produce_stored',  // json field with array data
                    'regions.name as regionName', 
                    'districts.name as districtName',
                    'users.name as agent_assigned',
                    'warehouses.status'
                    )
                ->get();