我有两个表“ teachers”和“ teacher_certificates”,其中包含教师证书。
我正在尝试通过口才如此的关系来保存教师证书,例如https://laravel.com/docs/5.7/eloquent-relationships#the-create-method
教师模型:
namespace App\Models\Teachers;
use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\SoftDeletes;
class Teacher extends Model
{
public function Certificates()
{
return $this->hasMany(TeacherCertificates::class, 'teacher_id');
}
}
}
TeacherCertificate模型:
namespace App\Models\Teachers;
use App\User;
use Illuminate\Database\Eloquent\Model;
class TeacherCertificates extends Model
{
protected $table='teacher_certificates';
protected $fillable=['teacher_id','certificate'];
protected $touches=[Teacher::class];
public $timestamps=false;
}
和控制器:
namespace App\Http\Controllers\Dashboard;
use App\Http\Controllers\Controller;
use App\Models\Teachers\Experiences;
use App\Models\Teachers\Skills;
use App\Models\Teachers\Teacher;
use App\Models\Teachers\TeacherCertificates;
use App\User;
use Illuminate\Http\Request;
use App\Http\Requests\TeacherCreateRequest;
use Illuminate\Support\Facades\DB;
class TeacherController extends Controller
{
public function store(TeacherCreateRequest $request)
{
DB::transaction(function () use ($request) {
$data = $request->all();
$user = User::create($data);
$data[ 'user_id']=$user->id;
$teacher = Teacher::create($data);
//certificates
$certificates=array_map(
function ($certificate) use ($teacher) {
return ['teacher_id' => $teacher->id,
'certificate' => $certificate];
}, explode('\n', $request->certifications));
$teacher->Certificates()->createMany($certificates);
}
}
}
我收到此错误:
BadMethodCallException
Method Illuminate\Database\Query\Builder::App\Models\Teachers\Teacher does not exist.