laravel-friendships icon indicating copy to clipboard operation
laravel-friendships copied to clipboard

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

Open jhm-ciberman opened this issue 6 years ago • 12 comments
trafficstars

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 avatar Mar 14 '19 01:03 jhm-ciberman

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

jhm-ciberman avatar Mar 14 '19 01:03 jhm-ciberman

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

hootlex avatar Mar 14 '19 17:03 hootlex

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!

jhm-ciberman avatar Mar 14 '19 21:03 jhm-ciberman

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.

Solomon04 avatar Mar 30 '19 05:03 Solomon04

Hi guys, any news on this issue? Thanks

MarcoCazzaro avatar Apr 09 '19 10:04 MarcoCazzaro

Still no news?

Baspa avatar Apr 29 '19 08:04 Baspa

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

jnbn avatar Aug 12 '19 00:08 jnbn

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": "::"
        },

patterueldev avatar Jan 08 '20 13:01 patterueldev

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

khacnha avatar Feb 16 '20 04:02 khacnha

Hey Friends has the issue been resolved ?

nelson1995 avatar Apr 26 '20 20:04 nelson1995

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

Solomon04 avatar Apr 27 '20 19:04 Solomon04

the contributors will must make update with code

tojo-r27 avatar May 28 '20 19:05 tojo-r27