【问题标题】:How can I display the array as table format in laravel view如何在 laravel 视图中将数组显示为表格格式
【发布时间】:2020-03-06 17:34:48
【问题描述】:

如何在 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"}}

这是我的控制器函数,它将数组返回给刀片。

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 | 

【问题讨论】:

    标签: php arrays laravel-5 laravel-blade


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-27
      • 2019-07-19
      • 2015-03-01
      • 2020-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多