0

I try to add value in DB, and after that to get the ID, and to use in another table ,for a column, but it's always null, and I am not sure why.

    $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...
    ]);
Beusebiu
  • 1,433
  • 4
  • 23
  • 68

1 Answers1

0

You are using save method which returns boolean. You need to use create method, which will return you the object.

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