Laravel航海者:BelongsToMany
Laravel Voyager: BelongsToMany
我有 table 本书和作者。我想获得将作者附加到书籍的能力。我为书籍调整 BREAD:
使用这些设置,我可以点击编辑并为我的书选择作者,但是当我尝试保存书籍时,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))
这是合乎逻辑的。但是当我将 name
切换到 id
时,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';
}
}
}
请帮忙!
样板书:
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/
我有 table 本书和作者。我想获得将作者附加到书籍的能力。我为书籍调整 BREAD:
使用这些设置,我可以点击编辑并为我的书选择作者,但是当我尝试保存书籍时,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))
这是合乎逻辑的。但是当我将 name
切换到 id
时,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';
}
}
}
样板书:
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/