Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stephen waweru's avatar

how to pass auto generated input to an api function in laravel

i have a web system whereby the input fields are autogenerated and saved in the database.i want to push the input data to an api function and have been able to push some of the inputs.i have which is autogenerated but am unable to push it.its the systemid.here is the code that generates it

  $SystemID        = new SystemID('product');
  $product         = new product();
  $product->systemid = $SystemID;
  $product->save();

i have tried this code in the api function of the controller but it shows an error systemid cannot be null

$productdetails=new Product;
$productdetails->systemid =$request->systemid;

how can i pass the autogenerated systemid to the controller in the api?

0 likes
12 replies
Nakov's avatar

You are not saving anything by doing this: $SystemID = new SystemID('product'); so there is no system id generated..

1 like
Nakov's avatar

@stephen waweru is this SystemID an eloquent model? And what is the product you are passing in the constructor?

  $SystemID        = new SystemID;
$SystemID->name = 'product';
$SystemID->save();

// now you can use the `$SystemID->id`.
1 like
stephen waweru's avatar

@Nakov nop the systemid isnt a model the product is a table..the SYSTEMID here is autogenerated and saved in the product table in the systemid column

Nakov's avatar

@stephen waweru show the SystemID class buddy.. the constructor should not return a value. So you are forgetting something there..

1 like
Nakov's avatar

@stephen waweru I wish I can ask where did you find this, and what are you working on.. but that will be waste of my time I think..

the $id that is generated is private to the SystemID class, so you cannot access it. At least I don't see a method on how you can access it.

There are just two methods that require some kind of $t_id which I have no clue what it is, but it also won't return an ID that you expect I guess.

Sorry, but I can't help with so many unknowns.

1 like
stephen waweru's avatar

@Nakov the systemid is created and saved in the products table perfectly.i have tested that and its working

Nakov's avatar

@stephen waweru so you are using this $request->systemid; in your "API Controller" but how are you sending it? Do you send it in the request body?

You are not showing any of your code.. just how you try to use it, which is hard to predict.

1 like
stephen waweru's avatar

@Nakov i was asking how i can request the systemid that was autogenerated and saved at the products table

Nakov's avatar

@stephen waweru for that you need to fetch the product by its id:

$productdetails= Product::find(1);

// here is your system id
dd($productdetails->systemid);

you cannot access a saved product with new Product that creates a NEW product..

1 like
stephen waweru's avatar

@Nakov i have tried this but it doesnt work,its throwing an error in the log file at the endpoint api

Too few arguments to function App\Http\Controllers\APIController::push_data()

Please or to participate in this conversation.