I have an array which I would like to output in the edit template in a foreach loop.
But I get an error cannot access offset of type string on string
In blade I have this:
@foreach ($post->cage as $c )
<div data-repeater-item="">
<div class="form-group">
<label class="col-sm-1 control-label">Make</label>
<label class="col-sm-1 control-label">Comment</label>
<div class="col-sm-2">
<input type="text" name="cage[1][comment]" value="{{ $c['comment'] }}" class="form-control">
</div>
</div>
</div>
@endforeach
The controller is very basic
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id): Response
{
return response()->view('post.edit', [
'post' => Posts::findOrFail($id),
]);
}
a vardump gives this
array(3) { ["level"]=> string(6) "middle" ["comment"]=> string(4) "joe2" [0]=> array(2) { ["level"]=> string(6) "middle" ["comment"]=> string(4) "joe2" } }
Any idea how to make the foreach to get the value?
Do I have to add something in the controller? decode or something?
Thanks in advance
Extra edit.
Cage is a text field which can store an array
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('posts', function (Blueprint $table) {
$table->text('zone')->nullable();
$table->text('cage')->nullable();
});
}
In the model file
protected $casts = [
'cage' => 'array'
];