I’m working on a laravel 10 project and I have issues on production (local server) but not in localhost (php artisan serve).
I have a livewire component that deals comment submitting on a request.
I have two models (User and Request) linked by a many to many relation with a pivot table (Guests).
There is my Request model:
<?php
namespace AppModels;
use AppModelsUser;
use AppModelsComment;
use AppModelsProject;
use AppModelsService;
use AppModelsCategory;
use AppModelsDocument;
use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentRelationsBelongsToMany;
use IlluminateDatabaseEloquentRelationsHasMany;
use IlluminateDatabaseEloquentRelationsBelongsTo;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentSoftDeletes;
class Request extends Model
{
use HasFactory, SoftDeletes;
[...]
public function guests() : BelongsToMany
{
return $this->belongsToMany(User::class, 'guests')->orderBy('name', 'asc');
}
public function hasGuest(User $user) : bool
{
return $this->guests()->where('user_id', $user->id)->exists();
}
}
My User mode :
<?php
namespace AppModels;
use AppModelsRole;
use AppModelsComment;
use AppModelsService;
use LaravelSanctumHasApiTokens;
use MBarlowMegaphoneHasMegaphone;
use IlluminateNotificationsNotifiable;
use IlluminateDatabaseEloquentSoftDeletes;
use IlluminateDatabaseEloquentRelationsHasMany;
use IlluminateDatabaseEloquentRelationsBelongsTo;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateDatabaseEloquentRelationsBelongsToMany;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, SoftDeletes, HasMegaphone;
[...]
public function invitedRequests(): BelongsToMany
{
return $this->belongsToMany(Request::class, 'guests');
}
}
And my livewire component :
<?php
namespace AppLivewire;
use AppModelsComment;
use AppServicesNotificationService;
use LivewireComponent;
use AppMegaphoneNewComment;
use IlluminateSupportFacadesLog;
use IlluminateSupportFacadesAuth;
class CommentInput extends Component
{
public $req;
public $comments;
public $id;
public $newComment;
protected $notificationService;
public function __construct()
{
$this->notificationService = new NotificationService();
}
public function render()
{
return view('livewire.comment-input');
}
public function submitComment()
{
if($this->newComment)
{
try {
$comm = Comment::create([
'content' => $this->newComment,
'user_id' => Auth::id(),
'request_id' => $this->id,
]);
} catch (Throwable $th) {
Log::error($th);
}
$this->reloadComments();
$this->newComment = '';
if($this->req->user_id != Auth::id()) {
$this->notificationService->sendCommentNotification($this->req->user, $this->req, $comm);
} elseif($this->req->affected_user_id != Auth::id()) {
$this->req->affectedUser ? $this->notificationService->sendCommentNotification($this->req->affectedUser, $this->req, $comm) : '';
} else if($this->req->service->manager_id != Auth::id()) {
$this->notificationService->sendCommentNotification($this->req->service->manager, $this->req, $comm);
}
$guests = $this->req->guests;
foreach($guests as $guest) {
if ($guest->id != Auth::id()
&& $guest != $this->req->user
&& $guest != $this->req->affectedUser
&& $guest != $this->req->service->manager)
{
$this->notificationService->sendCommentNotification($guest, $this->req, $comm);
}
}
}
}
[...]
}
Only on production it throw me this error :
Call to undefined relationship [guests] on model [AppModelsRequest].
I’ve verified that my migration ran correctly on production server and I have my new guests table with correct foreign keys.
My database has also InnoDB type.
I don’t know why it works on localhost but not on my linux server…