Reputation: 49
I have a consult, I need to pass the data of an array in the controller to the view, but when I get the data from the array in the view it gives me an error.
"Illegal string offset 'name'
Controller
for($i=0; $i < count($num); $i++) {
$data = [
'name' => $input['name'][$i],
'price' => $input['price'][$i],
'quantity' => intval($input['quantity'][$i]),
'created_at'=>$now,
'updated_at'=>$now,
];
}
return view('view', compact('data'));
View
@foreach($data as $dat)
<tr>
<td width="100">Product</td>
<td width="50">Quantity</td>
</tr>
<tr>
<td>{{ $dat['name'] }}</td>
<td>{{ $dat['quantity'] }}</td>
</tr>
@endforeach
dd($data)
array:5 [▼
"name" => "name1"
"price" => "14.00"
"quantity" => 1
"created_at" => "2018-12-11 09:03:35"
"updated_at" => "2018-12-11 09:03:35"
]
What am I wrong about?
Upvotes: 1
Views: 92
Reputation: 2292
Not sure but try this
$data = [];
for($i=0; $i < count($num); $i++) {
$temp = [
'name' => $input['name'][$i],
'price' => $input['price'][$i],
'quantity' => intval($input['quantity'][$i]),
'created_at'=>$now,
'updated_at'=>$now,
];
array_push($data,$temp);
}
return view('view', compact('data'));
Upvotes: 1
Reputation: 2683
You are passing right, problem is in your array. It isn't multidimensional, but you are trying to access it like multidimensional.
$data = [];
for($i=0; $i < count($num); $i++) {
$data[] = [
'name' => $input['name'][$i],
'price' => $input['price'][$i],
'quantity' => intval($input['quantity'][$i])
];
}
Upvotes: 0