【发布时间】:2021-10-01 16:56:57
【问题描述】:
我正在尝试从数据库表中提取最后插入的 id,以便可以将其输入到新的数据库表中,如下所示:
$mealplaninput =
MealPlanInput::create([
'meal_type' => $meal,
'suitable_for' => $suited,
'allergens' => $allerg,
'specific_allergen' => $spec,
'no_of_people' => $nop,
'start_date' => $request->date,
'no_of_days' => $nod,
'user_id' => $currentuserid,
]);
尝试拉取最后一个id(但不起作用):
$uniquemealplanid = $mealplaninput->id();
然后输入到新表中:
MealPlanDisplay::create([
'MealPlan_ID' => $uniquemealplanid,
'Day' => $recipeday,
]);
但是我得到了错误:
调用未定义的方法 App\Models\MealPlanInput::id()
我也试过其他方法,比如:
$uniquemealplanid = $this->create($mealplaninput)->id;
但我得到的错误是:
方法 App\Http\Controllers\MealPlanInputController::create 不存在。
如何从 MealPlanInput 中提取最后一个 id?
【问题讨论】:
-
id不是属性吗?试试$mealplaninput->id而不是$mealplaninput->id()。 -
我试过了,不知道为什么,但不幸的是它只是返回 null。
-
执行
dd($mealplaninput);并检查它实际包含的内容。据我所知,它应该返回填充了 id 的新模型。你的表的主键是id,还是你叫它别的名字?
标签: php sql database laravel eloquent