如何在laravel视图中以表格式显示数组

时间:2019-11-11 01:13:40

标签: php arrays laravel-5 laravel-blade

如何在Laravel中以表格格式显示在数组下面?下面是示例数组。

{"1":{"name":"Bajiya","quantity":2,"price":"34.00"},"2":{"name":"Gulha","quantity":2,"price":"3.00"}}

{"1":{"name":"Bajiya","quantity":2,"price":"34.00"},"2":"name":"Gulha","quantity":2,"price":"3.00"}}

{"1":{"name":"Bajiya","quantity":3,"price":"34.00"},"2":{"name":"Gulha","quantity":3,"price":"3.00"},"3":{"name":"kavaabu","quantity":2,"price":"2.00"}}

{"1":{"name":"Bajiya","quantity":2,"price":"34.00"}}

这是我的Controller Function,它将数组返回到刀片。

public function cartsIndex(Request $request)
{
    $carts = Cart::all('cart');
    $carts = json_decode($carts);
    $carts = Arr::pluck($carts, 'cart');
    return view('cartsIndex')->with(["carts" => $carts]);
}

这是我的查看文件

@extends('layouts.app')
@section('content')
@foreach ($carts as $cart => $name)
    {{ $name }} <br>
@endforeach
@endsection

这是我的模型文件     

namespace App;

use Illuminate\Database\Eloquent\Model;

class Cart extends Model
{
protected $fillable = ['cart',]; 
protected $casts = ['cart' =>'cart',];
}

我希望表格显示如下

| Name | Quantity | Price | 

1 个答案:

答案 0 :(得分:0)

我认为您没有正确设置foreach,请尝试以下操作

@extends('layouts.app')
@section('content')
@foreach ($carts as $cart)
    {{ $cart->name }} <br>
@endforeach
@endsection