Laravel Voyager:BelongsToMany

问题描述 投票:1回答:1

我有一本书桌和作者。我希望能够将作者与书籍联系起来。我调整了BREAD的书籍:enter image description here

通过这些设置,我可以单击编辑并为我的书选择作者,但是当我尝试保存书籍时,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';
    }
}
}

Pivot table请帮忙!

php laravel laravel-5 voyager
1个回答
2
投票

型号书籍:

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/

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.