If you use the render method for initializing and re-rendering the Collection of paymentMethods, then it will be re-evaluated whenever the component needs to re-render.
Property not found, on update
Completely new to LiveWire, but i like it. I have the following problem, i'm trying to make a card a default from a list of cards. The component is showing me all the card and it is updating the default card. The problem is when it tries to reload the list (after the update) is giving me the error that the card variable that i used doesnt exist; My code is very troglodyte but is the only way that i fund to work with Stripe: here is my livewire component:
<?php
namespace App\Http\Livewire;
use App\Models\User;
use Livewire\Component;
class CardList extends Component
{
public $paymentMethods;
public $user;
/**
* mount
*
* @return void
*/
public function mount()
{
$this->paymentMethods = collect();
$this->user = User::find(auth()->user()->id);
foreach ($this->user->paymentMethods() as $paymentMethod) {
$this->paymentMethods->push($paymentMethod->asStripePaymentMethod());
}
}
public function render()
{
return view('livewire.card-list');
}
public function makeDefault($cardId)
{
$this->user->updateDefaultPaymentMethod($cardId);
return redirect()->route('cardManagement');
}
}
here is the livewire blade:
<div>
@if (auth()->user()->hasPaymentMethod())
<div>
Active Cards
</div>
<table class="tbl">
<thead class="tbl-head">
<tr>
<th>Type</th>
<th>Number</th>
<th>Actions</th>
</tr>
</thead>
<tbody class="tbl-body">
@foreach ($paymentMethods as $card)
<tr>
<td>
@switch($card->card->brand)
@case('visa')
<i class="fab fa-cc-visa bg-blue-600"></i>
@break
@case('mastercard')
<i class="fab fa-cc-mastercard bg-orange-400"></i>
@break
@case('amex')
<i class="fab fa-cc-amex bg-blue-400"></i>
@break
@default
<i class="far fa-credit-card"></i>
@endswitch
</td>
<td> ****-****-****-{{$card->card->last4}} </td>
<td>
@if ($user->defaultPaymentMethod()->asStripePaymentMethod()->id == $card->id)
Default
@else
<div class="justify-between">
<button class="btn btn-info btn-xs" wire:click='makeDefault("{{$card->id}}")'>Make Default</button>
<button class="btn btn-danger btn-xs">Delete</button>
</div>
@endif
</td>
</tr>
@endforeach
</tbody>
</table>
<div class="ml-7">
<ul>
</ul>
</div>
@else
<div class="justify-items-center">
<div class="mt-4 text-center">
There are no Cards Associated with the account
</div>
<div class="mt-4 text-center">
Please enter one to start your subscription
</div>
</div>
@endif
</div>
If i cancel the error and call the route, it load with no problem and showing the correct default card. I'm guessing livewire is tryin to only update the rows that changed and is loosing the original variable values. Any ideas.
here is the error:
ErrorException
Trying to get property 'card' of non-object (View: C:\Users\Daniel.SFP\Documents\Projects\procalc\resources\views\livewire\card-list.blade.php)
http://127.0.0.1:8000/cardManagement
Please or to participate in this conversation.