简体   繁体   中英

Laravel : How do I display array data from database

I have tried to fetch encoded array data from the database, but I am getting unformatted array data.

Data that is displaying

{"no_of_rounds":"Round 1"}

Blade

 @forelse($orders as $order)
  {{ json_encode($order->no_of_rounds,true) }}
 @endforelse

Controller

 public function index()
    {
        $authUser = Auth::user();

        $orders = Item::where('email', $authUser->email)->orderBy('id', 'DESC')->paginate(5);
     
        // dd($nor);
        
        
      $currency = CurrencyHelper::getCurrencyString();
        return view('member.dashboard', compact('authUser','orders','currency'));
    }

Model

class Item extends Model
{
    use HasFactory;
    protected $casts = [
        'no_of_rounds' => 'collection',//For collection
        'no_of_rounds' => 'array'//For array
    ];
}

What I am expecting to get is a formatted data array

Round 1

Database在此处输入图片说明

For some reason you have written @forelse instead of @foreach

 @forelse($orders as $order)
  {{ json_encode($order->no_of_rounds,true) }}
 @endforeach

Try:

 @foreach($orders as $order)
 {{ $order->no_of_rounds }}
 @endforeach

Try this. After Save, show me raw data from database

Model

class Item extends Model
{
    use HasFactory;
    protected $casts = [
        'no_of_rounds' => 'array',
    ];
}

Blade

@foreach($orders as $order)
   @foreach ($order->no_of_rounds as $option)
      {{ $option }
   @endforeach
 @endforeach

Controller save

$nor = array(
            'no_of_rounds' => $request->no_of_rounds,
       );
 $data= New Item();
$data->no_of_rounds = $nor;
$data->save();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM