简体   繁体   English

我如何在控制器内的 Laravel 的不同表中获取自动递增 id 的值作为外键

[英]how can i get the value of an an auto incremented id as a foreign key in a different table in Laravel inside a controller

In the table called template, I get the id of staffmembers by using session.在名为模板的表中,我通过使用会话获取staffmembers的 id。 However, I really don't know how I can to it for the table module.但是,我真的不知道如何将它用于 table 模块。 I am coding using the Laravel framework.我正在使用 Laravel 框架进行编码。

I also get this error message :我也收到此错误消息:

Invalid datetime format: 1366 Incorrect integer value: 'templates_id' for column 'templates_id' at row 1 (SQL: insert into modules ( moduleName , moduleCode , year , templates_id )无效的日期时间格式:1366 不正确的整数值:第 1 行的“templates_id”列的“templates_id”(SQL:插入modulesmoduleNamemoduleCodeyeartemplates_id

template::create([
  'paperTitle' => $request->paperTitle,
  'markedElementOne' => $request->markedElementOne,
  'scoreOne' => $request-> scoreOne,
  'markedElementTwo' => $request->markedElementTwo,
  'scoreTwo' => $request-> scoreTwo,
  'staffmembers_id'=>\Session::get('staffmembers_id'),
]);

module::create([
  'moduleName' => $request->moduleName,
  'moduleCode' => $request->moduleCode,
  'year' => $request->year,
  'templates_id'=>('templates_id'),
]);

Get the template back to a variable when creating.创建时将模板恢复为变量。

$tempalte = template::create([
    'paperTitle'       => $request->paperTitle,
    'markedElementOne' => $request->markedElementOne,
    'scoreOne'         => $request-> scoreOne,
    'markedElementTwo' => $request->markedElementTwo,
    'scoreTwo'         => $request-> scoreTwo,
    'staffmembers_id'  => \Session::get('staffmembers_id'),
    ]);

get template id from that variable to use to create 'module'从该变量中获取模板 ID 以用于创建“模块”

module::create([
    'moduleName'   => $request->moduleName,
    'moduleCode'   => $request->moduleCode,
    'year'         => $request->year,
    'templates_id' => $template->id
    ]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM