2016-12-01 2 views
3

두 모델 : UserForm이 있습니다.Laravel Eloquent relationship - 이상한 경로

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가 null 허용 정수 열 경우 : Form 모델은 두 개의 belongsTo 관계를 맺고있다.

$manager = App\User::findOrFail(1); 
$form = App\Form::findOrFail(1); 
$form->manager_user()->assign($manager); 

을하지만 난 오류 얻을 :

장인 어설프게을 사용하여, 나는 폼에 관리자로 사용자를 할당하려고 (these methods 사용) 내가 잘못

$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\User 대신 App\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); 
} 
} 
+1

아마도 네임 스페이스 문제 일 수 있습니다. –

+2

모델의 네임 스페이스를 표시 할 수 있습니까? –

+0

확실히 제 편집을 참조하십시오. – Gacek

답변

3

당신은 가능성이 상대적으로 네임 스페이스의 클래스와 네임 스페이스 해상도 문제가 참고 문헌 App\User a Laravel의 App\FormNamespace Resolution rules

에서

From Laravel Docs

  1. 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.

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.

중 하나를 당신의 UserForm 클래스 참조하기 전에 App\ 네임 스페이스 선언을 제거하거나 다른 \로 접두사를하는 것은 그들이 완전하게하려고합니다.

+0

예. 내 대답도 참조하십시오. – Gacek

0

@Kevin Stitch는 내가 상대적인 네임 스페이스에 문제가 있다고 제안했습니다. 내 Form 모델에서

나는 절대 경로를 가지고 관계를 조정 :

class Form extends Model 
{ 
public function user(){ 
    return $this->belongsTo("\App\User"); 
} 

public function manager_user(){ 
    return $this->belongsTo("\App\User", 'manager_id'); 
} 
} 

을 그리고 모든 (장인 어설프게을 다시 시작한 후) 잘 작동합니다.