laravel-friendships
laravel-friendships copied to clipboard
Does not work with Laravel 5.8 (Event::fire)
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;
}
´´´
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 the PR is incomplete. When it's fixed and tests pass for all supported Laravel versions I will merge ASAP
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!
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.
Hi guys, any news on this issue? Thanks
Still no news?
As no PR's are being accepted you can replace all ::fire methods with ::dispatch in your Friendable trait manually.
As no PR's are being accepted you can replace all
::firemethods with::dispatchin 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": "::"
},
Me too!
"message": "Method Illuminate\\Events\\Dispatcher::fire does not exist.",
Hey Friends has the issue been resolved ?
@jpteruel095 you don't have to edit your vendor file. Just extend the class and override the methods.
the contributors will must make update with code