I have two models: User and Form. The Form model has two belongsTo relationships:
class Form extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public function manager_user()
{
return $this->belongsTo(User::class, 'manager_id');
}
}
manager_id is a nullable integer column.
Using artisan tinker, I try to assign a user as a manager to a form (using these methods):
$manager = App\User::findOrFail(1);
$form = App\Form::findOrFail(1);
$form->manager_user()->assign($manager);
but I get error:
$form->manager_user()->associate($gacek)
PHP Fatal error: Class 'App\App\User' not found in /var/www/html/test/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 779
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'App\App\User' not found
What am I doing wrong? Why is the framework trying to search for App\App\User instead of App\User?
It's a fresh installation of Laravel 5.3.
EDIT Full model files with namespaces:
Form model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Form extends Model
{
public function user(){
return $this->belongsTo("App\User");
}
public function manager_user(){
return $this->belongsTo("App\User", 'manager_id');
}
}
User model:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password', 'surname', 'login', 'sign'
];
protected $hidden = [
'password', 'remember_token',
];
public function forms(){
return $this->hasMany(Form::class);
}
}
You likely have a namespace resolution issue with the relative namespace class references App\User and App\Form with Laravel.
By default, this directory is namespaced under App and is autoloaded by Composer using the PSR-4 autoloading standard. You may change this namespace using the app:name Artisan command.
From Laravel Docs
- Relative names always resolve to the name with namespace replaced by the current namespace. If the name occurs in the global namespace, the namespace\ prefix is stripped. For example namespace\A inside namespace X\Y resolves to X\Y\A. The same name inside the global namespace resolves to A.
From Namespace Resolution rules
Try either removing the App\ namespace declaration before your User and Form class references or prefix them with another \ to make them fully qualified.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With