2

I am getting the next error when trying to display a view in Laravel :

"Cannot use object of type stdClass as array (View: C:\xampp\htdocs\mysite\resources\views\cms\contactus.blade.php)".

My Controller:

public function contactus(){ 

    ContactUS::get_content(self::$data);
    return view('cms.contactus', self::$data); 

} 

My Model:

class ContactUS extends Model
{

public $table = 'contactus';

public $fillable = ['name','email','message']; 

static public function get_content(&$data){ 


        $sql = "SELECT cu.*,name,email,message FROM contactus cu "
            . "ORDER BY cu.created_at DESC ";

        $data['contactusd'] = DB::select($sql); 

     }

} 

My view:

@extends ('cms.cms_master') 
@section('cms_content')
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h1 class="page-header">Contact us form information</h1>
    @if($contactusd) 
    <br><br>
    <table class="table table-bordered">  
        <thead>
            <tr>  
                <th>Name</th> 
                <th>Email</th> 
                <th>Message</th> 
            </tr>
        </thead> 
        @foreach($contactusd as $item)  
        <tr>
            <td>{{ $item['name']}}</td> 
            <td>
                <ul>

                    <li> Email: {{ $item['email']}}, Massage: {{$item['message'] }}</li>
                    @endforeach
                </ul>
            </td> 
            <td>{{ $item[created_at]}}</td> 
        </tr>

    </table> 
    @else 
    <p style="font-size: 18px">No information...</p>
    @endif
</div> 

@endsection
3
  • 1
    Well, you can use it as an object instead ($item->email), or convert it to an array @foreach($contactusd as (array)$item) Commented Sep 7, 2017 at 12:28
  • 1
    Possible duplicate of How do I convert an object to an array? Commented Sep 7, 2017 at 12:28
  • nah he is asking for laravel and however you should prefer eloquent instead of Raw Queries. Commented Sep 7, 2017 at 12:39

5 Answers 5

7

To convert an object to array (as is your question) use $array = (array) $object;

Sign up to request clarification or add additional context in comments.

Comments

1

you have hell lot of mistake in your blade file

@if($contactusd) 
    <br><br>
    <table class="table table-bordered">  
        <thead>
            <tr>  
                <th>Name</th> 
                <th>Email</th> 
                <th>Message</th> 
            </tr>
        </thead> 
        @foreach($contactusd as $item)  
        <tr>
            <td>{{ $item->name }}</td> 
            <td>
                <ul>

                    <li> Email: {{ $item->email}}, Massage: {{$item->message }}</li>

                </ul>
            </td> 
            <td>{{ $item->created_at}}</td> 
        </tr>
        @endforeach
    </table> 
    @else 
    <p style="font-size: 18px">No information...</p>
    @endif

or

$contactusd = ContactUS::get_content(self::$data)->toArray();
return view('cms.contactus', $contactusd);

Comments

1

Try this..

$sql = "SELECT cu.*,name,email,message FROM contactus cu "
            . "ORDER BY cu.created_at DESC ";

$data['contactusd'] = DB::select($sql)->get()->toArray();

Comments

1

If you getting data is in std class , you can write like this in view page {{ $item->name }}

If you getting data is in std class , you can write like this in view page {{$item['message']}}

backside write like this

$sql = "SELECT cu.*,name,email,message FROM contactus cu "
            . "ORDER BY cu.created_at DESC ";

$data['contactusd'] = DB::select($sql)->get()->toArray();

Comments

0

use like this

$data = ContactUS::get_content(self::$data)->toArray();
return view('cms.contactus', $data);

Comments

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.