Coder Social home page Coder Social logo

Comments (3)

driesvints avatar driesvints commented on July 24, 2024

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.

yanalshoubaki avatar yanalshoubaki commented on July 24, 2024

@driesvints

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.

driesvints avatar driesvints commented on July 24, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.