Ad

Laravel Request + For Loop

- 1 answer

I'm trying to make object's of an incoming formrequest like this:

for ($i=0; $i <= $totalProducts; $i++) { 
      $products[$i] = new product($request->title+$i,$request->description+$i,$request->price+$i,$request->number+$i,$request->total+$i);
     }

But when I for example dd($request->title1); I receive an output. But when I do dd($request->title+$i) or dd($request->title.$i) it's only showing the number.

How can I use $i with a $request?

Ad

Answer

Try using $request->input('title'.$i) instead.

The reason dd($request->title.$i) is not working is that it acts as if you are asking the request object to return the title then concatenate to the result the value of $i, and since there is no value for title in the request object, nothing is returned or empty space and concatenated to it the value of $i.

Ad
source: stackoverflow.com
Ad