Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zaster's avatar

syntax error, unexpected '{' (View: ...\views\livewire\account-show.blade.php)

Livewire Component

<?php

namespace App\Http\Livewire;

use App\Models\Account;
use App\Models\AccountTransaction;
use Livewire\Component;
use Livewire\WithPagination;
use Symfony\Component\Console\Descriptor\Descriptor;

class AccountShow extends Component
{
    use WithPagination;

    public $sortBy = 'created_at';
    public $sortAsc = false;
    public $search = '';
    public $perPage = 10;
    public $account;
    public $accountTransactions;
    //public $runningBalance = [];
    public $runningBalance;


    // Table Sort
    public function sortBy($field)
    {
        if($field == $this->sortBy){
            $this->sortAsc = !$this->sortAsc;
        }
            $this->sortBy = $field;
    }

    public function updatingSearch()
    {
        $this->resetPage();
    }

    public function mount(Account $account)
    {
        $this->account = $account ?? new Account();

        //Selected Account's accounttransactions
        $this->accountTransactions = $account->accountTransactions;

        // foreach ($this->accountTransactions as $accountTransaction) {
        // //dd($accountTransaction->id);
        //     $this->runningBalance[$accountTransaction->id] += $accountTransaction->amount;
        // }

        foreach ($this->accountTransactions as $accountTransaction) {
            $this->runningBalance += $accountTransaction->amount;
        }
    }

    public function render()
    {
        $accountTransactions = AccountTransaction::with('account', 'transaction')
        ->where('account_id', $this->account->id)
        ->orderBy($this->sortBy, $this->sortAsc ? 'ASC' : 'DESC')
        ->paginate($this->perPage);
        return view('livewire.account-show', [
            'accountTransactions' =>$accountTransactions,
        ]);
    }
}

<div class="flex flex-col">

    <div class="xl:flex xl:items-center xl:justify-between xl:mx-4 xl:mb-2">
        <p class="text-3xl ..."></p>
        <div class="xl:w-4/6">
            <div class="flex flex-col space-y-2 xl:space-x-2 xl:flex xl:flex-row xl:space-y-0">

                <select wire:model="perPage"
                    class="block border-gray-300 rounded-md shadow-sm 1/6 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">
                    <option>10</option>
                    <option>100</option>
                    <option>1000</option>
                </select>


            </div>
        </div>
    </div>

    <div class="overflow-x-auto xl:-my-2 sm:-mx-6 lg:-mx-8">
        <div class="inline-block min-w-full py-2 align-middle sm:px-6 lg:px-8">
            <table class="min-w-full text-center divide-y divide-gray-200 table-fixed">
                <thead class="font-bold text-gray-700 bg-gray-50">
                        <th scope="col" class="w-3/12 px-6 py-3 text-xs tracking-wider uppercase">
                            <div class="flex items-center justify-center">
                                <div class="flex items-center justify-center">
                                    transaction date
                                </div>
                            </div>
                        </th>
                        <th scope="col" class="w-3/12 px-6 py-3 text-xs tracking-wider uppercase">
                            <div class="flex items-center justify-center">
                                <div class="flex items-center justify-center">
                                    description
                                </div>
                            </div>
                        </th>
                        <th scope="col" class="w-2/12 px-6 py-3 text-xs tracking-wider uppercase">
                            <div class="flex items-center justify-center">
                                <button wire:click="sortBy('amount')" class="text-xs font-bold tracking-wider uppercase">amount</button>
                                <x-sort-icon icon sort-field="amount" :sort-by="$sortBy" :sort-asc="$sortAsc" />
                            </div>
                        </th>
                        <th scope="col" class="w-4/12 px-6 py-3 text-xs tracking-wider uppercase">
                            <div class="flex items-center justify-center">
                                <div class="flex items-center justify-center">
                                    running balance
                                </div>
                            </div>
                        </th>
                </thead>
                <tbody class="text-left bg-white divide-y divide-gray-200">
                    @foreach ($accountTransactions as $accountTransaction)
                    <tr>
                                <td class="px-6 py-2 text-sm text-gray-500 border-2 border-gray-200 whitespace-nowrap text-center">
                                    {{ $accountTransaction->created_at }}
                                </td>
                                <td class="px-6 py-2 text-sm text-gray-500 border-2 border-gray-200 whitespace-nowrap text-center">
                                    {{ $accountTransaction->description }}
                                </td>
                                <td class="px-6 py-2 text-sm text-gray-500 border-2 border-gray-200 whitespace-nowrap text-right">
                                    {{ number_format($accountTransaction->amount, 2) }}
                                </td>
                                <td class="px-6 py-2 text-sm text-gray-500 border-2 border-gray-200 whitespace-nowrap text-center">
                                    {{ number_format($runningBalance.{{ $index }}.amount, 2) }}
                                </td>
                                <td class="px-6 py-2 text-sm text-gray-500 border-2 border-gray-200">
                                    <div class="h-10 overflow-x-hidden overflow-y-auto">

                                    </div>
                                </td>
                            </tr>
                        @endforeach
                </tbody>
            </table>

         <div class="mx-4 mt-4">
            {{ $accountTransactions->links() }}
        </div>

    </div>


</div>


Account Model

    public function accountTransactions()
    {
        return $this->hasMany('App\Models\AccountTransaction', 'account_id');
    }

Table: account_transaction

Columns:
id bigint(20) UN AI PK 
account_id int(11) 
transaction_id int(11) 
resource_id int(11) 
comp_id int(11) 
description varchar(255) 
amount int(11) 
created_at timestamp 
updated_at timestamp 
deleted_at timestamp
0 likes
17 replies
tykus's avatar

A lot or commented code in that view template….

Tray2's avatar

Like @tykus says, a lot of commented out code. I would start by removing all the the "dead" code. The error message usually gives you a line number as well so start there.

zaster's avatar

@tykus @tray2 Sorry about that.

Cleared the comments from the view

The issue should be in this part

 {{ number_format($runningBalance.{{ $index }}.amount, 2) }}

I have tried two approaches in the livewire class

  1. The array approach (In comments - which is not functional)
  2. The collection approach
zaster's avatar

@mohamedtammam @tray2

  {{ number_format($runningBalance->index->amount, 2) }}

and it gives

Trying to get property 'index' of non-object

tykus's avatar

@zaster what is $runningBalance, an array?

{{ number_format($runningBalance[$index]['amount'], 2) }}
// or using `data_get`
{{ number_format(data_get($runningBalance, $index . '.amount'), 2) }}
zaster's avatar

@tykus

$runningBalance is a collection

{{ number_format($runningBalance[$index]['amount'], 2) }}
// or using `data_get`
{{ number_format(data_get($runningBalance, $index . '.amount'), 2) }}

`Tried both and i get the below

Undefined variable: index

tykus's avatar

@zaster you had a $index varable here

 {{ number_format($runningBalance.{{ $index }}.amount, 2) }}
zaster's avatar

@tykus

I am using this for the first time in my life and have no idea how it works yet.

zaster's avatar

@tykus

This works but the purpose of the running balance fails.

I need to have a collection or an array and display the data in a table line by line

For example if this is the $runningBalance array

[100, 250, 500, 400]

I need to display those line by line and it should relate with $accountTransaction as per the foreach loop

tykus's avatar

@zaster so this should get what?

foreach ($this->accountTransactions as $accountTransaction) {
   $this->runningBalance->push($accountTransaction->amount + $this->runningBalance->sum());
}
zaster's avatar

@tykus

 public function mount(Account $account)
    {
        $this->account = $account ?? new Account();
    
        $this->accountTransactions = $account->accountTransactions;

        foreach ($this->accountTransactions as $accountTransaction) {
            $this->runningBalance += $accountTransaction->amount;
        }

        dd($this->runningBalance);
    }

returns an integer

with my data it returns

506
zaster's avatar

@tykus Yes you are correct. It is a variable. It was a collection a while ago LOL. Me so stupid.

Please or to participate in this conversation.