Comments (3)
Heya, thanks for reporting.
We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.
laravel new bug-report --github="--public"
Please do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.
Thanks!
from framework.
This is my code that have the issue, when there is not much caching on the page, it's work correctly :)
But when there is much caching on the page, it's not working
this is my code for the component that is not working on it
<?php
namespace App\Livewire\Component;
use App\Models\Message;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Livewire\Attributes\On;
use Livewire\Component;
class MessageWithReplay extends Component
{
public Message $message;
public ?User $authUser = null;
public Collection $likes;
public Collection $favorites;
public int $likes_count = 0;
public bool $liked = false;
public int $favorites_count = 0;
public bool $favorited = false;
public $usersLike;
public $messageDetails = [];
public function mount(Message $message)
{
$this->message = $message;
$this->authUser = Cache::remember('user:' . Auth::id(), now()->addHours(4), function () {
return Auth::user();
});
$this->likes = Cache::remember("message:{$message->id}:likes", now()->addHours(4), function () {
return $this->message->likes;
});
$this->favorites = Cache::remember("message:{$message->id}:favorites", now()->addHours(4), function () {
return $this->message->favorites;
});
$this->likes_count = $this->likes->count();
$this->favorites_count = $this->favorites->count();
if ($this->authUser) {
$this->liked = $this->message->likes->contains('user_id', $this->authUser->id);
$this->favorited = $this->message->favorites->contains('user_id', $this->authUser->id);
}
$this->messageDetails = [
'title' => $this->message->message,
'url' => route('message.show', ['message' => $this->message]),
];
}
#[On('add-like')]
public function refreshLikes()
{
Cache::forget("message:{$this->message->id}:likes");
$this->liked = $this->message->likes->contains('user_id', $this->authUser->id);
$this->likes_count = $this->likes->count();
}
#[On('add-favorite')]
public function refreshFavorites()
{
Cache::forget("message:{$this->message->id}:favorites");
$this->favorited = $this->message->favorites->contains('user_id', $this->authUser->id);
$this->favorites_count = $this->favorites->count();
}
public function addLike()
{
if ($this->liked) {
$this->message->likes()->where('user_id', $this->authUser->id)->delete();
$this->dispatch('add-like');
return;
}
$this->message->likes()->create([
'user_id' => $this->authUser->id,
]);
$this->dispatch('add-like');
}
public function addFavorite()
{
if ($this->favorited) {
$this->message->favorites()->where('user_id', $this->authUser->id)->delete();
$this->dispatch('add-favorite');
return;
}
$this->message->favorites()->create([
'user_id' => $this->authUser->id,
]);
$this->dispatch('add-favorite');
}
public function render()
{
return view('livewire.component.message-with-replay', [
'message' => $this->message,
'likes' => $this->likes,
'liked' => $this->liked,
'favorites' => $this->favorites,
'favorited' => $this->favorited,
'likes_count' => $this->likes_count,
'favorites_count' => $this->favorites_count,
'message_details' => $this->messageDetails,
]);
}
}
When I click on the button that has addLike
function then will create a new like or deleted it, then forget the cache for the key "message:{$this->message->id}:likes", and get the new count for likes ( execute new query ) and reflected it on the same component.
** If there is another way to do caching, please let me know.
** edit : I tried something else to update likes data :
... mount function
$this->likes = Cache::remember("message:{$message->id}:likes", now()->addHours(4), function () {
return $this->message->likes()->get();
});
$this->favorites = Cache::remember("message:{$message->id}:favorites", now()->addHours(4), function () {
return $this->message->favorites()->get();
});
....
#[On('add-like')]
public function refreshLikes()
{
Cache::forget("message:{$this->message->id}:likes");
$this->likes = Cache::remember("message:{$this->message->id}:likes", now()->addHours(4), function () {
return $this->message->likes()->get();
});
$this->liked = $this->likes->contains('user_id', $this->authUser->id);
$this->likes_count = $this->likes->count();
}
....
#[On('add-favorite')]
public function refreshFavorites()
{
Cache::forget("message:{$this->message->id}:favorites");
$this->favorites = Cache::remember("message:{$this->message->id}:favorites", now()->addHours(4), function () {
return $this->message->favorites()->get();
});
$this->favorited = $this->favorites->contains('user_id', $this->authUser->id);
$this->favorites_count = $this->favorites->count();
}
Is this the best solution for fixing the issue ?
Thank you
from framework.
@yanalshoubaki there's way too much going on in this code example that's not related to the problem at hand. Please create a minimal reproduction repo with the CLI command from above for us to debug.
from framework.
Related Issues (20)
- Partial translation with validation rule required_if
- Misleading error message when using auth:sanctum middleware HOT 1
- [11.11] Cannot bind an instance to a static closure HOT 5
- [10.x] collection shift can not handle an empty collection HOT 1
- Validator Error message not being created correctly HOT 8
- The Laravel new command does not work when Herd is not installed HOT 3
- Job gets stuck in pending when dependent model is deleted inside the handle method. HOT 2
- Laravel 11.10 "artisan about" issue HOT 2
- SQLite queries generated by Eloquent with non existing columns in WHERE clause should generate an error/exception but don't HOT 1
- Laravel 10 mailer failover inline image crackd. when the first mail send fail HOT 3
- `Bus::chain` Skipping jobs with readonly non-scalar properties HOT 3
- RequiredIf validator with multiple values fails validating HOT 1
- Incorrect Sorting with UUID Data Type in MariaDB HOT 11
- orWhere ignore the first condition (where)
- Web and API non existing route files usage does not trigger an error when loaded from bootstrap/app file HOT 1
- `view()` is rendered when passed as component attribute HOT 1
- 2006 MySQL server has gone away in queue:work scenario HOT 1
- Closure in job chain is not called if preceded by job batch HOT 2
- Error with AJAX request HOT 1
- Custom cast class overrides the related column HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from framework.