RTippin / messenger

Laravel messenger. A full messenger suite for your new / existing laravel app! Private and group threads between multiple models, with real-time messaging, reactions, attachments, calling, chat bots, and more!
https://tippindev.com
MIT License
417 stars 84 forks source link

App\Models\User::name must return a relationship instance. #6

Closed lps83 closed 3 years ago

lps83 commented 3 years ago

Hi, Thanks for you amazing work ! I trying to make it work on one of my project !

Sadly I get stuck with this error.

[2021-03-18 20:27:02] local.ERROR: App\Models\User::name must return a relationship instance. {"userId":26,"exception":"[object] (LogicException(code: 0): App\\Models\\User::name must return a relationship instance. at /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:464)

I put that in my User Model because I use Username instead of 'name'

public function name(): string { return strip_tags(ucwords($this->username)); }

Any idea ? Thanks

RTippin commented 3 years ago

Does your user model already have a name() method or interface that returns a relationship? Mine only ever expects to return a string, so I find your error odd. Can you show more of your stack trace? I'd like to know if the error is being thrown from my package or your app in general.

lps83 commented 3 years ago

Yes, I only have one public function name() in App/Models/User I may made a mistake during the configuration ... I don't have an attribute name. I use "username"

Stacktrace :

`[2021-03-21 20:01:16] local.ERROR: App\Models\User::name must return a relationship instance. {"userId":26,"exception":"[object] (LogicException(code: 0): App\Models\User::name must return a relationship instance. at /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:464) [stacktrace]

0 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(441): Illuminate\Database\Eloquent\Model->getRelationshipFromMethod('name')

1 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(396): Illuminate\Database\Eloquent\Model->getRelationValue('name')

2 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1785): Illuminate\Database\Eloquent\Model->getAttribute('name')

3 /Users/sylvain/Documents/dev/obpro/vendor/laravel/jetstream/src/HasProfilePhoto.php(69): Illuminate\Database\Eloquent\Model->__get('name')

4 /Users/sylvain/Documents/dev/obpro/vendor/laravel/jetstream/src/HasProfilePhoto.php(59): App\Models\User->defaultProfilePhotoUrl()

5 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(494): App\Models\User->getProfilePhotoUrlAttribute(NULL)

6 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(508): Illuminate\Database\Eloquent\Model->mutateAttribute('profile_photo_u...', NULL)

7 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(162): Illuminate\Database\Eloquent\Model->mutateAttributeForArray('profile_photo_u...', NULL)

8 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1325): Illuminate\Database\Eloquent\Model->attributesToArray()

9 /Users/sylvain/Documents/dev/obpro/vendor/rtippin/messenger/src/Http/Resources/ProviderResource.php(77): Illuminate\Database\Eloquent\Model->toArray()

10 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Collections/helpers.php(184): RTippin\Messenger\Http\Resources\ProviderResource->RTippin\Messenger\Http\Resources\{closure}()

11 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Http/Resources/ConditionallyLoadsAttributes.php(105): value(Object(Closure))

12 /Users/sylvain/Documents/dev/obpro/vendor/rtippin/messenger/src/Http/Resources/ProviderResource.php(78): Illuminate\Http\Resources\Json\JsonResource->when(true, Object(Closure))

13 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Http/Resources/Json/JsonResource.php(95): RTippin\Messenger\Http\Resources\ProviderResource->toArray(Object(Illuminate\Http\Request))

14 /Users/sylvain/Documents/dev/obpro/vendor/rtippin/messenger/src/Http/Resources/ProviderStatusResource.php(51): Illuminate\Http\Resources\Json\JsonResource->resolve()

15 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Http/Resources/Json/JsonResource.php(95): RTippin\Messenger\Http\Resources\ProviderStatusResource->toArray(Object(Illuminate\Http\Request))

16 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Http/Resources/Json/ResourceResponse.php(39): Illuminate\Http\Resources\Json\JsonResource->resolve(Object(Illuminate\Http\Request))

17 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Http/Resources/Json/JsonResource.php(221): Illuminate\Http\Resources\Json\ResourceResponse->toResponse(Object(Illuminate\Http\Request))

18 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Routing/Router.php(750): Illuminate\Http\Resources\Json\JsonResource->toResponse(Object(Illuminate\Http\Request))

19 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Routing/Router.php(737): Illuminate\Routing\Router::toResponse(Object(Illuminate\Http\Request), Object(RTippin\Messenger\Http\Resources\ProviderStatusResource))

20 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Routing/Router.php(691): Illuminate\Routing\Router->prepareResponse(Object(Illuminate\Http\Request), Object(RTippin\Messenger\Http\Resources\ProviderStatusResource))

21 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))

22 /Users/sylvain/Documents/dev/obpro/vendor/rtippin/messenger/src/Http/Middleware/SetMessengerProvider.php(47): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))

23 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): RTippin\Messenger\Http\Middleware\SetMessengerProvider->handle(Object(Illuminate\Http\Request), Object(Closure), 'required')

24 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))

25 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))

26 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(127): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))

27 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(103): Illuminate\Routing\Middleware\ThrottleRequests->handleRequest(Object(Illuminate\Http\Request), Object(Closure), Array)

28 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(55): Illuminate\Routing\Middleware\ThrottleRequests->handleRequestUsingNamedLimiter(Object(Illuminate\Http\Request), Object(Closure), 'messenger-api', Object(Closure))

29 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Routing\Middleware\ThrottleRequests->handle(Object(Illuminate\Http\Request), Object(Closure), 'messenger-api')

30 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(44): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))

31 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Auth\Middleware\Authenticate->handle(Object(Illuminate\Http\Request), Object(Closure))

32 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(77): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))

33 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))

34 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))

35 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))

36 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Session/Middleware/AuthenticateSession.php(58): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))

37 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Session\Middleware\AuthenticateSession->handle(Object(Illuminate\Http\Request), Object(Closure))

38 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(121): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))

39 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(63): Illuminate\Session\Middleware\StartSession->handleStatefulRequest(Object(Illuminate\Http\Request), Object(Illuminate\Session\Store), Object(Closure))

40 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))

41 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))

42 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))

43 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(67): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))

44 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))

45 /Users/sylvain/Documents/dev/obpro/vendor/rtippin/messenger/src/Http/Middleware/MessengerApi.php(31): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))

46 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): RTippin\Messenger\Http\Middleware\MessengerApi->handle(Object(Illuminate\Http\Request), Object(Closure))

47 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))

48 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Routing/Router.php(693): Illuminate\Pipeline\Pipeline->then(Object(Closure))

49 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Routing/Router.php(668): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))

50 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Routing/Router.php(634): Illuminate\Routing\Router->runRoute(Object(Illuminate\Http\Request), Object(Illuminate\Routing\Route))

51 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Routing/Router.php(623): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))

52 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(166): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))

53 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))

54 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))

55 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))

56 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))

57 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))

58 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))

59 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))

60 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(87): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))

61 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance->handle(Object(Illuminate\Http\Request), Object(Closure))

62 /Users/sylvain/Documents/dev/obpro/vendor/fruitcake/laravel-cors/src/HandleCors.php(57): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))

63 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fruitcake\Cors\HandleCors->handle(Object(Illuminate\Http\Request), Object(Closure))

64 /Users/sylvain/Documents/dev/obpro/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))

65 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fideloper\Proxy\TrustProxies->handle(Object(Illuminate\Http\Request), Object(Closure))

66 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))

67 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(141): Illuminate\Pipeline\Pipeline->then(Object(Closure))

68 /Users/sylvain/Documents/dev/obpro/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))

69 /Users/sylvain/Documents/dev/obpro/public/index.php(52): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))

70 /Users/sylvain/Documents/dev/obpro/server.php(21): require_once('/Users/sylvain/...')

71 {main}

"} `

RTippin commented 3 years ago

Did you fix it? My package requires your provider models to implement my contract, which requires the name() method. So if you already had a name method on your model that did not return a string, that will cause conflicts. I have not used jetstream, but I am unsure why I see 'name' being loaded on your model, and as a relationship. Seems my package is conflicting with something else you had on your user model?

lps83 commented 3 years ago

I'm still working on it. No your fonction name() is the only one in my user's model.

public function name(): string { return strip_tags(ucwords($this->username)); }

RTippin commented 3 years ago

Can you share your user model then? Also, what happens if you grab a user model and dump($user->name()) and dump($user->username) ? I have no issues myself using my name method or overwriting it in my projects that use this package.

lps83 commented 3 years ago

This is my user model :

`<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Illuminate\Support\Facades\Auth; use Laravel\Fortify\TwoFactorAuthenticatable; use Laravel\Jetstream\HasProfilePhoto; use Laravel\Sanctum\HasApiTokens; use Spatie\Permission\Traits\HasRoles; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use RTippin\Messenger\Contracts\MessengerProvider; use RTippin\Messenger\Contracts\Searchable; use RTippin\Messenger\Traits\Messageable; use RTippin\Messenger\Traits\Search;

class User extends Authenticatable implements MessengerProvider, Searchable { use HasApiTokens; use HasFactory; use HasProfilePhoto; use HasRoles; use Notifiable; use TwoFactorAuthenticatable; use Messageable; use Search;

private $attached;
private $expire;
private $highlevel;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'username',
    'fname',
    'lname',
    'email',
    'password',
    'ipv4',
    'color',

];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password',
    'pin',
    'remember_token',
    'two_factor_recovery_codes',
    'two_factor_secret',
];

/**
 * The attributes that should be cast to native types.
 *
 * @var array
 */
protected $casts = [
    'email_verified_at' => 'datetime',
];

/**
 * The accessors to append to the model's array form.
 *
 * @var array
 */
protected $appends = [
    'profile_photo_url',
];

static public function userAttachedFullName(){
}
static public function userAttachedShortName(){
}
static public function getCoiffeurs(){
}
static public function getCoiffeursId(){
}
static public function getCoiffeursJSON(){
}
static public function getStaffUsername($id){
}
static public function getUserById($id){
}
static public function isAttachedCan($doWhat){
}
static public function prestaFullOrNot($array){
}
static public function getAgendasData($id){
}
static public function getStoreName(){
}

//Messenger
public function getAvatarColumn(): string
{
    return 'profile_photo_path';
}
public function name(): string
{
    return strip_tags(ucwords($this->username));
}

} `

lps83 commented 3 years ago

dd(strip_tags(ucwords($this->username))) works and return my username.

RTippin commented 3 years ago

What are these coming from?

static public function userAttachedFullName(){
}
static public function userAttachedShortName(){
}
RTippin commented 3 years ago

I know nothing about jetstream, but looking at the stacktrace, my provider resource will return your base model, ( https://github.com/RTippin/messenger/blob/master/src/Http/Resources/ProviderResource.php#L77 ) and laravel is calling some jetstream code for your picture, which seems to reference name, which you do not even have on your model as you changed that (lname, fname, username).

https://github.com/laravel/jetstream/blob/2.x/src/HasProfilePhoto.php#L69

tapang786 commented 2 years ago

InvalidArgumentException

The given provider { App\Models\User } must implement the interface RTippin\Messenger\Contracts\MessengerProvider

at vendor/rtippin/messenger/src/Messenger.php:92 88▕ } 89▕ 90▕ foreach ($providers as $provider) { 91▕ if (! is_subclass_of($provider, MessengerProvider::class)) { ➜ 92▕ throw new InvalidArgumentException("The given provider { $provider } must implement the interface ".MessengerProvider::class); 93▕ } 94▕ 95▕ $this->providers[$provider] = new MessengerProviderDTO($provider); 96▕ }

  +1 vendor frames 

2 app/Providers/MessengerServiceProvider.php:28 Illuminate\Support\Facades\Facade::__callStatic("registerProviders")

  I apply all things 
  but when i run php artisan serve this error 
RTippin commented 2 years ago

@tapang786 Please do not reply to old issues that have nothing in common with your issue at hand, on top of opening a new issue.