我有兩個型號:User
和Form
。該Form
模型有兩個belongsTo
關係:Laravel雄辯的關係 - 奇怪的路徑
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
是空整型列。
使用Artisan鼓搗,我嘗試將用戶作爲經理分配到形式(使用these methods):
$manager = App\User::findOrFail(1);
$form = App\Form::findOrFail(1);
$form->manager_user()->assign($manager);
,但我得到的錯誤:
$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
我在做什麼錯?爲什麼框架試圖搜索App\App\User
而不是App\User
?
這是Laravel 5.3的全新安裝。
編輯 完整模型命名空間的文件:
Form
型號:
<?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
型號:
<?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);
}
}
也許命名空間問題。 –
你可以顯示你的模型的命名空間! –
當然,請參閱我的編輯。 – Gacek