Reputation: 453
Want to fetch array on laravel, The Document is
{
"_id" : ObjectId("5b030a1b5085321e84006eff"),
"name" : "Amalendu kar",
"Information" : {
"email" : "[email protected]",
"comment" : "Ok",
"website" : "example.com",
"info" : {
"x" : 203,
"y" : 102
},
"versions" : [
"0.9.7",
"0.9.8",
"0.9.9"
]
},
"updated_at" : ISODate("2018-05-21T18:04:11Z"),
"created_at" : ISODate("2018-05-21T18:04:11Z")
}
and i ausing this command to save this
public function store(Request $request)
{
request()->validate([
'name' => 'required',
'email' => 'required',
'website' => 'required',
'comment' => 'required',
'gender' => 'required',
]);
//Data::insert($request->all());
Data::create(['name' => $request->input('name'),
"Information"=>[
"email" => $request->input('email'),
"comment" => $request->input('comment'),
'website' =>$request->input('website'),
"info" => (object)array( "x" => 203, "y" => 102),
"versions" => array("0.9.7", "0.9.8", "0.9.9")
]
]);
return redirect('/') ->with( 'success', ' Done! ');
}
Now i want to fetch 'name' field and 'email' from Infromation here is the code but i am getting error.
@foreach ($data as $value)
<h1>{{ $value->name }}</h1>
{{$content[] = $value->Information }}
@foreach ($content as $key)
<h1>{{ $key->email }}</h1>
@endforeach
@endforeach
Getting this error
"htmlspecialchars() expects parameter 1 to be string,
Upvotes: 0
Views: 3512
Reputation: 453
It is working this way only
@foreach ($data as $value )
<h1>{{$value['name']}}</h1>
<p>{{ $value->Information['email'] }}</p>
@endforeach
Upvotes: 1
Reputation: 2488
If you use Laravel-MongoDb library, use eloquent methods to fetch the data as a collection like below:
$data = Data::where('name',$value)->first();
return $data->name; // must return name
return $data->information['email']; // must return email
Also if you want to get Information as a collection too, you must define relationships or convert to collection yourself with the Laravel collect(array)
method.
It must be OK.
Also, try indexes in the bracket in your code as below,
@foreach ($data as $value)
<h1>{{ $value['name'] }}</h1>
{{$content[] = $value['Information'] }}
@foreach ($content as $key)
<h1>{{ $key['email'] }}</h1>
@endforeach
@endforeach
Upvotes: 0