0

I am trying to access the properties of an array from a view. A controller is passing the array to that view. For some reason am not able to access the the array properties.

Error Message:

Trying to get property 'message' of non-object (View: /path/to/file/message.blade.php)

View code + where error is occurring:

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">


            <div class="card">
                <div class="card-header">{{$message->title}}</div>

                <div class="card-body">
                    {{$message->message}}
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Controller code that is returning the above view:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Auth;
use App\User;

class becomeorganiser extends Controller
{
    public function becomeorganiser(){
      $user = Auth::user();
      $user->organiser = 1;
      $user->save();
      $message = [];
      $message['title'] = 'Success!';
      $message['message'] = 'You are now an event organiser<br>You now have access the oragnisers control panel in your navigation bar!';

      return view('message', $message);
    }
}

If I do {{print_r($message)}} The contents is printed out. To be clear I cannot access either the title or message properties

What am I doing wrong?

Raul Sauco
  • 2,645
  • 3
  • 19
  • 22
ICP
  • 97
  • 10
  • Please do not edit your question based on the answers that you receive, you changed `message->title` to `message[0]` and then `message['title']` based on Script46's answer. As a result your question, and the answer, do not make any sense before going through the edits. – Raul Sauco Apr 27 '18 at 23:24
  • Sorry, I was trying stuff to get it to work and forgot to put the changes back. I never intended to alter the code. I only meant to add code that was around what I though was the problem code. – ICP Apr 27 '18 at 23:28

1 Answers1

4

$message is not an object, yet you are accessing it as one. It is an array as you have defined it as such in your controller ($message = [];), therefore you need to access it as such.

So, it should be like this,

<div class="card-header">{{ $message['title'] }}</div>    
<div class="card-body">
      {{ $message['message'] }}
</div>

Hence, the error:

Trying to get property 'message' of non-object (View: /path/to/file/message.blade.php)

is completely valid.

Reading Material

array

object

Edit #1

Regarding your new error,

Illegal string offset 'title' (View:.....

As per my comment and your update, you are using numeric keys yet the array has been defined as an associative array. Please read above again this time noticing how I am accessing the values from the array.

Script47
  • 14,230
  • 4
  • 45
  • 66
  • I don't understand what you mean. Are you telling me it needs to be an object that pass and not an array? – ICP Apr 27 '18 at 22:46
  • @ICP No. I'm saying exactly the opposite, look at your code, you have passed an array and then are trying to access it as an object. You need to access it as an array. Am I making sense? – Script47 Apr 27 '18 at 22:47
  • Ok, I understand now. However, I am getting this error instead by using your code "Illegal string offset 'title' (View:....." – ICP Apr 27 '18 at 22:53
  • @ICP are those the only values you are putting in to `$message` or is there anything else? Try doing `var_dump` to see if the values are being placed in to the array correctly. – Script47 Apr 27 '18 at 22:54
  • those three lines are it. Message does not exist before that first line. – ICP Apr 27 '18 at 22:56
  • array(2) { ["title"]=> string(8) "Success!" ["message"]=> string(106) "You are now an event organiser You now have access the oragnisers control panel in your navigation bar!" } – ICP Apr 27 '18 at 22:57
  • @ICP please see [this](https://stackoverflow.com/a/20271518/2263631) to see if you are doing anything like it and without seeing more of your code / a verifiable example, I don't think I can help much further as I can only work of the errors that are provided. – Script47 Apr 27 '18 at 23:00
  • I have updated my post with all the code, but doubt it will help. – ICP Apr 27 '18 at 23:07
  • @ICP please look at my code then look at your code. You are using numeric keys yet you have created an associative array. Please try the code in *my* post. Notice my **Edit #1** too. – Script47 Apr 27 '18 at 23:08
  • ignore that. I was trying it with indexes instead. I will edit now – ICP Apr 27 '18 at 23:12
  • @ICP I don't understand, are you using numeric keys or no? If not, is the error still showing? – Script47 Apr 27 '18 at 23:13
  • both numeric keys and those string keys give same error. – ICP Apr 27 '18 at 23:15
  • using the "array(... ) format also gives the same error. – ICP Apr 27 '18 at 23:16
  • blade is executing it like this "" is this correct? – ICP Apr 27 '18 at 23:22