0

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,
3
  • Which library do you use to connect Laravel and MongoDB? Commented May 21, 2018 at 18:51
  • i am using jenssegers/laravel-mongodb Commented May 21, 2018 at 19:10
  • Check my answer if it was not enough, say me to explain more! Commented May 21, 2018 at 19:12

2 Answers 2

1

It is working this way only

@foreach ($data as $value )
    <h1>{{$value['name']}}</h1>
    <p>{{ $value->Information['email'] }}</p>
@endforeach
Sign up to request clarification or add additional context in comments.

1 Comment

sorry, I have but not getting how to do it
0

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

1 Comment

Not working. I am fetching all the entire collection $data = Data::all();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.