hootlex / laravel-friendships

This package gives Eloquent models the ability to manage their friendships.
MIT License
703 stars 150 forks source link

Does not work with Laravel 5.8 (Event::fire) #125

Open jhm-ciberman opened 5 years ago

jhm-ciberman commented 5 years ago

Error: Call to undefined method Illuminate\Events\Dispatcher::fire() Laravel Version: 5.8

The method ´Event::fire()´ has been deprecated (and removed) It should be replaced with ´Event::dispatch()´

´´´php

/**
 * @param Model $recipient
 *
 * @return \Hootlex\Friendships\Models\Friendship|false
 */
public function befriend(Model $recipient)
{

    if (!$this->canBefriend($recipient)) {
        return false;
    }

    $friendship = (new Friendship)->fillRecipient($recipient)->fill([
        'status' => Status::PENDING,
    ]);

    $this->friends()->save($friendship);

    Event::fire('friendships.sent', [$this, $recipient]); // HERE!!!!!!!!!! 

    return $friendship;

}

´´´

jhm-ciberman commented 5 years ago

I think this PR solves the issue. Please merge this asap, since my clients app deppends on this package

https://github.com/hootlex/laravel-friendships/pull/124

hootlex commented 5 years ago

@jhm-ciberman the PR is incomplete. When it's fixed and tests pass for all supported Laravel versions I will merge ASAP

jhm-ciberman commented 5 years ago

For my scenario, I created my own "HasFriends" trait and extended yours overwriting the conflictive methods. So, its not that urgent for me at the moment. Thanks for the quick answer!

Solomon04 commented 5 years ago

Building on @jhm-ciberman solution I also created a separate trait, here is a what I did:

Create Trait

<?php

namespace App\Traits;

use Hootlex\Friendships\Models\Friendship;
use Hootlex\Friendships\Models\FriendFriendshipGroups;
use Hootlex\Friendships\Status;
use Hootlex\Friendships\Traits\Friendable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Event;

trait FriendableTempFix
{
    use Friendable;

    /**
     * @param Model $recipient
     * @return bool|Friendship
     */
    public function befriend(Model $recipient)
    {
        if (!$this->canBefriend($recipient)) {
            return false;
        }

        $friendship = (new Friendship)->fillRecipient($recipient)->fill([
            'status' => Status::PENDING,
        ]);

        $this->friends()->save($friendship);

        Event::dispatch('friendships.sent', [$this, $recipient]);

        return $friendship;
    }

    /**
     * @param Model $recipient
     * @return mixed
     */
    public function unfriend(Model $recipient)
    {
        $deleted = $this->findFriendship($recipient)->delete();

        Event::dispatch('friendships.cancelled', [$this, $recipient]);

        return $deleted;
    }

    /**
     * @param Model $recipient
     * @return mixed
     */
    public function acceptFriendRequest(Model $recipient)
    {
        $updated = $this->findFriendship($recipient)->whereRecipient($this)->update([
            'status' => Status::ACCEPTED,
        ]);

        Event::dispatch('friendships.accepted', [$this, $recipient]);

        return $updated;
    }

    /**
     * @param Model $recipient
     * @return mixed
     */
    public function denyFriendRequest(Model $recipient)
    {
        $updated = $this->findFriendship($recipient)->whereRecipient($this)->update([
            'status' => Status::DENIED,
        ]);

        Event::dispatch('friendships.denied', [$this, $recipient]);

        return $updated;
    }

    /**
     * @param Model $recipient
     * @return Friendship
     */
    public function blockFriend(Model $recipient)
    {
        // if there is a friendship between the two users and the sender is not blocked
        // by the recipient user then delete the friendship
        if (!$this->isBlockedBy($recipient)) {
            $this->findFriendship($recipient)->delete();
        }

        $friendship = (new Friendship)->fillRecipient($recipient)->fill([
            'status' => Status::BLOCKED,
        ]);

        $this->friends()->save($friendship);

        Event::dispatch('friendships.blocked', [$this, $recipient]);

        return $friendship;
    }

    /**
     * @param Model $recipient
     * @return mixed
     */
    public function unblockFriend(Model $recipient)
    {
        $deleted = $this->findFriendship($recipient)->whereSender($this)->delete();

        Event::dispatch('friendships.unblocked', [$this, $recipient]);

        return $deleted;
    }
}

I am overriding the methods and replaced Event::fire() with Event::dispatch()

Replace Friendable Trait in User Model

Then instead of using the Friendable trait in the user model, use the new trait you just created in place of the Friendable trait.

Note this is just a temporary solution, once the PR fix has been merged, I recommend upgrading your vendor package.

MarcoCazzaro commented 5 years ago

Hi guys, any news on this issue? Thanks

Baspa commented 5 years ago

Still no news?

jnbn commented 4 years ago

As no PR's are being accepted you can replace all ::fire methods with ::dispatch in your Friendable trait manually.

patterueldev commented 4 years ago

As no PR's are being accepted you can replace all ::fire methods with ::dispatch in your Friendable trait manually.

So you have to replace it inside the vendors folder? Not too practical

Btw I'm using Laravel 6 and the issue still exists.

{
    "message": "Call to undefined method Illuminate\\Events\\Dispatcher::fire()",
    "exception": "Symfony\\Component\\Debug\\Exception\\FatalThrowableError",
    "file": "{rootfolder}/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php",
    "line": 239,
    "trace": [
        {
            "file": "{rootfolder}/vendor/hootlex/laravel-friendships/src/Traits/Friendable.php",
            "line": 35,
            "function": "__callStatic",
            "class": "Illuminate\\Support\\Facades\\Facade",
            "type": "::"
        },
khacnha commented 4 years ago

Me too! "message": "Method Illuminate\\Events\\Dispatcher::fire does not exist.",

nelson1995 commented 4 years ago

Hey Friends has the issue been resolved ?

Solomon04 commented 4 years ago

@jpteruel095 you don't have to edit your vendor file. Just extend the class and override the methods.

tojorodialson commented 4 years ago

the contributors will must make update with code