Home > Back-end >  Laravel compact array of two items failed when showing it in blade view
Laravel compact array of two items failed when showing it in blade view

Time:05-25

I have a function that accept an id and get the event with this id

Inside this event there is instructor column that carries the name of the instructor

I am getting the instructor column and search with it in the instructors table and get the instructor row

Now I need to compact them all to a view, so I did this

public function event($id) {
    $event = Event::where('id', '=', $id)->first();

    $instructor = Instructor::where('name', '=', $event->instructor);

    $data = [
        'event' => $event,
        'instructor' => $instructor
    ];

    return view('event', compact('data'));
}

View

<h2>{{ $data->event->name }}</h2>

But it gives me this error exception Attempt to read property "event" on array

I made sure that the id is really id not null

And I made sure that there is an event and an instructor

How to fix that?

CodePudding user response:

$data is an array , write like this:

  $data['event']->name
  • Related