Arrays:
[
{"id":1,"group":"post","name":"Mi","value":"x3 poco"},
{"id":2,"group":"post","name":"iPhone","value":"14 pro"},
{"id":3,"group":"post","name":"Nokia","value":"C5"},
{"id":4,"group":"post","name":"Dell","value":"15 insp"}
.
.
.
]
IndexController.php file:
$post = Post::all();
return view('index', compact('post'));
index.blade.php file:
Post Name: "{{ $post->name['iPhone']->value }}"
I want to show the output as below:
Post Name: "14 pro"
But error is showing:
Error: Property [name] does not exist on this collection instance.
CodePudding user response:
You have an array of arrays, so name
is inside a numeric index...
This:
[
{"id":1,"group":"post","name":"Mi","value":"x3 poco"},
{"id":2,"group":"post","name":"iPhone","value":"14 pro"},
{"id":3,"group":"post","name":"Nokia","value":"C5"},
{"id":4,"group":"post","name":"Dell","value":"15 insp"}
.
.
.
]
Is this on PHP:
$array = [
[
'id' => 1,
'group' => 'post',
'name' => 'Mi',
'value' => 'x3 poco',
],
[
// ...
],
];
So, you have to iterate it like this:
Posts:
@foreach ($post as $item)
Post Name: "{{ $item->value }}"
@endforeach
CodePudding user response:
You can use where
on the collection to search by name:
Post Name: "{{ $post->where('name', 'iPhone')->first()?->value }}"
The question mark is in case there is no item with the name you are looking for.