首页 > 解决方案 > 列未保存在数据库中,Laravel

问题描述

我尝试在 DB 中添加值,然后获取 ID,并在另一个表中使用列,但它始终为空,我不知道为什么。

    $lkp_location = new LkpLocation();
    if (is_numeric($request->lkp_location_text)) {  // If I get number from request, the value exists in DB, Else create and return ID
        $lkp_location_id = $request->lkp_location_text;
    } else {
        $lkp_location->fill([
            'text' => $request->lkp_location_text,
        ])->save();
        $lastInsertedId = $lkp_location->id;
        $lkp_location_id = $lastInsertedId;
    }
    dump($lkp_location_id); //It's the ID from DB

    $person = new Person();
    $person->fill([
        //code with another columns...
        'lkp_location_id' => $lkp_location_id,
        //code with another columns...
    ]);

标签: laravellaravel-6

解决方案


您正在使用返回布尔值的保存方法。您需要使用 create 方法,该方法将返回您的对象。

$lkp_location->fill([
            'text' => $request->lkp_location_text
])->create();

推荐阅读