【问题标题】: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如何在控制器内的 Laravel 的不同表中获取自动递增 id 的值作为外键
【发布时间】:2018-09-28 15:13:24
【问题描述】:

在名为模板的表中,我通过使用会话获得了staffmembers 的ID。但是,我真的不知道如何将它用于 table 模块。我正在使用 Laravel 框架进行编码。

我也收到此错误消息:

无效的日期时间格式: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'),
]);

【问题讨论】:

  • 'templates_id'=>('templates_id') - 这里的值是一个字符串,并且错误状态是一个整数,因此将其更改为应该插入的整数值。

标签: php mysql laravel controller


【解决方案1】:

创建时将模板取回变量。

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

从该变量中获取模板 ID 以用于创建“模块”

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

【讨论】:

  • 谢谢。请将此作为您的“已接受答案”
猜你喜欢
  • 2014-07-01
  • 1970-01-01
  • 2016-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-13
  • 2021-07-03
  • 1970-01-01
相关资源
最近更新 更多