2017-12-13 13 views
0

나는 테이블 서적과 작가가있다. 저자에게 책을 첨부 할 수있는 권한을 얻고 싶습니다. 책 I 조정 빵 : enter image description hereLaravel Voyager : BelongsToMany

내가 편집을 클릭하고 내 책의 저자를 선택할 수 있습니다 이러한 설정으로

,하지만 난 책을 저장하려고하면, laravel이 오류가 밖으로 준다 :

SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'Mrs. Alivia 
Stanton IV' for column 'authors_id' at row 1 (SQL: insert into 
`authors_books` (`authors_id`, `books_id`) values (Mrs. Alivia Stanton IV, 
55)) 

그리고 이것은이다 논리적이다. 내가 idname을 전환 할 때, laravel이 오류를 제공합니다

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in field 
list is ambiguous (SQL: select `id` from `authors` inner join 
`authors_books` on `authors`.`id` = `authors_books`.`authors_id` where 
    `authors_books`.`books_id` = 57) (View: 

나는 모든 조합을 사용하려고했으나 아무 일도하지 않습니다.

모델 책 :

class Books extends Model 
{ 
protected $fillable = ['name','img']; 

public function authors() 
{ 
    return $this->belongsToMany('App\Authors'); 
} 

public function getImgAttribute($img) 
{ 
    if(!empty($img)){ 
     return 'storage/images/books/'.$img; 
    }else{ 
     return 'storage/images/books/not-found.jpg'; 
    } 
} 

} 

모델 저자 :

class Authors extends Model 
{ 

protected $fillable = ['name','img']; 

public function books() 
{ 
    return $this->belongsToMany('App\Books'); 
} 

public function getImgAttribute($img) 
{ 
    if(!empty($img)){ 
     return 'storage/images/authors/'.$img; 
    }else{ 
     return 'storage/images/books/not-found.jpg'; 
    } 
} 
} 

Pivot table 도와주세요!

+0

책에 저자를 부착 할 때 코드를 입력하십시오 . – dexterb

+0

'id'를 선택할 때 테이블을 지정해야하므로 다음과 같아야합니다. 'authors.id'from 'authors'[...] ' –

+0

@dexterb 모델이 올바르게 설정되었으므로 정상적으로 작동합니다 보이저에,하지만 메신저 모델 코드를 넣으십시오 – NONAME

답변

1

모델 책 :

class Books extends Model 
{ 
protected $fillable = ['name','img']; 

public function authors() 
{ 
    return $this->belongsToMany('App\Author','Pivot table'); 
} 

public function getImgAttribute($img) 
{ 
    if(!empty($img)){ 
     return 'storage/images/books/'.$img; 
    }else{ 
     return 'storage/images/books/not-found.jpg'; 
    } 
} 

} 

모델 저자 :

class Authors extends Model 
{ 

protected $fillable = ['name','img']; 

public function books() 
{ 
    return $this->belongsToMany('App\Book,'Pivot table'); 
} 

public function getImgAttribute($img) 
{ 
    if(!empty($img)){ 
     return 'storage/images/authors/'.$img; 
    }else{ 
     return 'storage/images/books/not-found.jpg'; 
    } 
} 
} 

더 나은 이해를 위해이 문서를 읽어 보시기 바랍니다 http://laraveldaily.com/pivot-tables-and-many-to-many-relationships/

+0

정말? 내가 말했듯이, 사이트가 잘 작동하고 있습니다. 문서에 따르면 : 바인딩 테이블을 지정할 수 없습니다. 작성한 코드 외에도 두 번째 인수는 필드가 아닌 피벗 테이블입니다. 더 나은 이해를 위해이 문서를 읽으십시오. http://laraveldaily.com/pivot-tables-and-many-to-many-relationships/ – NONAME

+0

NONAME 죄송합니다. 내 나쁜 –

+0

이 대답은 나를 위해 작동합니다 – Sithu