Laravel 自定义数据透视表名称出现错误没有这样的表:main

Laravel 自定义数据透视表名称出现错误没有这样的表:main,laravel,eloquent,Laravel,Eloquent,使用自定义数据透视表名称实现多对多关系是一个错误 Service.php模型 class Service extends Model { public function categories() { return $this->belongsToMany('App\ServiceCategory', 'category_service'); } } class ServiceCategory extends Model { public function se

使用自定义数据透视表名称实现多对多关系是一个错误

Service.php模型

class Service extends Model
{
  public function categories()
  {
      return $this->belongsToMany('App\ServiceCategory', 'category_service');
  }
}
class ServiceCategory extends Model
{
  public function services()
  {
      return $this->belongsToMany('App\Service', 'category_service');
  }
}
ServiceCategory.php模型

class Service extends Model
{
  public function categories()
  {
      return $this->belongsToMany('App\ServiceCategory', 'category_service');
  }
}
class ServiceCategory extends Model
{
  public function services()
  {
      return $this->belongsToMany('App\Service', 'category_service');
  }
}
表名

  • 服务
  • 服务类别
  • 类别服务(透视表名称)
  • 试验

    测试给出了这个错误

    PDOException: SQLSTATE[HY000]: General error: 1 no such table: main.categories
    

    如何使用自定义透视表名称而不出现此错误。

    将第三个和第四个参数传递给包含相关模型外键的关系。在ServiceCategory模型中,尝试添加以下行:protected$table='yourTableName'@哦,谢谢。应用你的建议。但是,错误保持不变。将第三个和第四个参数传递给包含相关模型外键的关系。在ServiceCategory模型中,尝试添加以下行:protected$table='yourTableName'@哦,谢谢。应用你的建议。但是,错误保持不变。
    PDOException: SQLSTATE[HY000]: General error: 1 no such table: main.categories