1

I am trying to pass an array to blade view in Laravel. Here is my code in controller:

$event_nav= array();
       $event_nav[] = DB::table('tr_visit')
           ->join ('tm_child','tr_visit.Child_ID','=','tm_child.Child_ID')
           ->where('tr_visit.Child_ID', 'LIKE', '%' . $childName . '%')
           ->select(DB::raw('YEAR(Visit_Date)'))
           ->distinct()
           ->get();
 return view('ProfilAnak.BmiAnak.seeBmi',compact('event_nav'));

and here is my view:

    @foreach($event_nav as $year)
       {{$event_nav[0]}}
    @endforeach

I'm getting following error message :

"htmlentities() expects parameter 1 to be string, array given"

can anyone help?

3 Answers 3

1

you have to do something like this

@foreach($event_nav[0] as $year)
       {{$year->Child_ID}}
       {{$year->blah}}
@endforeach
Sign up to request clarification or add additional context in comments.

3 Comments

in @event_nav i just want select Year of Visit Date.. so how can i make it in view? {{$year->Visit_Date}} not working
->select(DB::raw('YEAR(Visit_Date) AS Visit_Date'))
Thanks for that - I'll change it.
1

What you were doing over here is same as echoing an array using echo so you need to update your code as

@foreach($event_nav as $year)
   {{$year->Visit_Date}}
@endforeach

Comments

0

replace

->select(DB::raw('YEAR(Visit_Date)'))

with

->select(DB::raw('YEAR(Visit_Date) AS Visit_Date'))

and in view do this

@foreach($event_nav as $year)
   {{$year->Visit_Date}}
@endforeach

like @Narendra Sisodia said

1 Comment

Thanks for that - I'll change it.

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.