how do you change segment value?
Livewire component update with new data
Hello,
I need help with the Livewire component update based on the select dropdown.
<?php
namespace App\Livewire\Future;
use Carbon\Carbon;
use Livewire\Component;
use App\Models\EodFuture;
class FutureTreeMapChart extends Component
{
public $segment = 'N50';
public $expiry = '2024-02-25';
public $data;
public function fetchTreeMap()
{
$bn = ['HDFCBANK', 'ICICIBANK'];
$n50 = [
'ADANIPORTS',
'ADANIENT',
];
if (!empty($this->segment) && !empty($this->expiry)) {
//Select for N50
if ($this->segment == 'N50') {
for ($i = 0; $i <= 5; $i++) {
$date = Carbon::today()->subDays($i)->format('Y-m-d');
// Check if data exists for the current date
if (EodFuture::whereDate('date', $date)->exists()) {
// If data exists for the current date, but the expiry date is gone, switch to next month
if (Carbon::parse($date)->endOfDay()->isPast()) {
$nextMonthStart = Carbon::now()->addMonth()->startOfMonth();
$nextMonthEnd = Carbon::now()->addMonth()->endOfMonth();
$this->data = EodFuture::select('asset_symbol', 'pchange')
->whereIn('asset_symbol', $n50)
->whereBetween('expiry', [$nextMonthStart, $nextMonthEnd])
->latest('date')
->orderBy('pchange', 'desc')
->take(count($n50))
->get();
break;
} else {
// If data exists for the current date and the expiry date is not yet past, return it
$this->data = EodFuture::select('asset_symbol', 'pchange')
->whereIn('asset_symbol', $n50)
->whereBetween('expiry', [now()->startOfMonth(), now()->endOfMonth()])
->latest('date')
->orderBy('pchange', 'desc')
->take(count($n50))
->get();
break;
}
}
}
}
//Select for BN.
else {
for ($i = 0; $i <= 5; $i++) {
$date = Carbon::today()->subDays($i)->format('Y-m-d');
// Check if data exists for the current date
if (EodFuture::whereDate('date', $date)->exists()) {
// If data exists for the current date, but the expiry date is gone, switch to next month
if (Carbon::parse($date)->endOfDay()->isPast()) {
$nextMonthStart = Carbon::now()->addMonth()->startOfMonth();
$nextMonthEnd = Carbon::now()->addMonth()->endOfMonth();
$this->data = EodFuture::select('asset_symbol', 'pchange')
->whereIn('asset_symbol', $bn)
->whereBetween('expiry', [$nextMonthStart, $nextMonthEnd])
->latest('date')
->orderBy('pchange', 'desc')
->take(count($bn))
->get();
break;
} else {
// If data exists for the current date and the expiry date is not yet past, return it
$this->data = EodFuture::select('asset_symbol', 'pchange')
->whereIn('asset_symbol', $bn)
->whereBetween('expiry', [now()->startOfMonth(), now()->endOfMonth()])
->latest('date')
->orderBy('pchange', 'desc')
->take(count($bn))
->get();
break;
}
}
}
}
}
$this->dispatch('dataUpdated');
}
public function render()
{
$this->fetchTreeMap();
return view('livewire.future.future-tree-map-chart');
}
}
Now in the component blade file,
<div class="card">
<div class="card-header">
<h3 class="card-title">Futures Treemap</h3>
<div class="card-toolbar">
<div class="d-flex align-items-center gap-2 gap-lg-3">
<select class="form-select form-select-solid" aria-label="Select example" wire:model.live="segment">
<option disabled>Segment</option>
<option value="N50">Nifty50</option>
<option value="BN">BankNifty</option>
</select>
<select class="form-select form-select-solid" aria-label="Select example" wire:model.live="expiry">
<option disabled>Expiry</option>
<option value="2024-03-28">2024-03-28</option>
<option value="2024-04-25">2024-04-25</option>
</select>
</div>
</div>
</div>
@if ($data)
<div class="card-body" id="chart">
</div>
@endif
</div>
@script
<script>
$wire.on('dataUpdated', () => {
var rawData = {!! $data->toJson() !!};
console.log(rawData);
console.log('updated');
});
</script>
@endscript
Whenever I change segment or expiry I can see there is an update request sent to the server and I get a response, but the problem is that rawData always logs for public $segment = 'N50';, Even if I select a different option for ```segment`` it logs for N50 only,
I did dd($this->data) on change, and it gets correct data from the database based on the selected segment, but on script, it always prints one which is selected as public property,
To me, it looks like some issues in Lifecycle, but I could not point out why rawData always get the default ```segment`` data,
What is wrong?
Thanks,
@LaCoder you should pass the updated data in the event.
Please or to participate in this conversation.