<?php
use App\Repositories\APIModels\SalesError as SalesError;
use App\Repositories\APIModels\Sales as Sales;
class SalesErrorTest extends \TestCase {
public function setUp()
{
parent::setUp();
}
public function testCreate()
{
$sales_error = new SalesError;
$sales_error->sale_id = 5;
$sales_error->error_type = 3;
$sales_error->description = 'Unknown error occured';
$sales_error->resolved_at = date('Y-m-d H:i:s');
$this->assertTrue($sales_error->save());
}
public function testRelation()
{
$sales = Sales::find(45)->errors();
var_dump($sales);
}
}
在上面的测试中,当我运行testCreate
时,它运行正常。但当我运行其他方法时,它说
PHP致命错误:找不到类'SalesError' /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php 在878号线上
销售模式
/**
* Has Many relation with SalesError
*
* @return object
*/
public function errors()
{
return $this->hasMany('SalesError', 'sale_id', 'sale_id');
}
销售错误模型
/**
* Has many to one relation with Sales Model
*
* @return void
*/
public function sales()
{
$this->belongsTo('Sales', 'sale_id');
}
答案 0 :(得分:1)
您应该将完整的namespace
传递给关系。
此外,您错过了return
关系中的sales
声明。
public function errors()
{
return $this->hasMany('App\Repositories\APIModels\SalesError', 'sale_id', 'sale_id');
// or
return $this->hasMany(SalesError::class, 'sale_id', 'sale_id');
}
public function sales()
{
return $this->belongsTo(Sales::class, 'sale_id');
}